gpt4 book ai didi

php - 错误 :NO database is selected

转载 作者:行者123 更新时间:2023-11-30 23:49:31 27 4
gpt4 key购买 nike

我试过它连接到数据库连接到数据库是成功的但是当我尝试将用户信息与数据库匹配时它给我一个错误没有选择数据库我尝试使用不同的方法连接到数据库,但没有任何效果

<?php
//CREATING CONNECTION TO DATABASE
$con= new mysqli("localhost", "****", "***", "*****");
$con->select_db("lel_server_user_db_secured");
if(mysqli_connect_errno())
{
echo "Problem With connection to database Please contact administrator regarding this error";
}
/* RETURNS NAME OF DEFAULT DATABASE
if ($result = $con->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("Default database is %s.\n", $row[0]);
$result->close();
}
*/
/*
$host="localhost";
$db_user="sky.xpert";
$db_pass="havefun344";
$database="lel_server_user_db_secured";
mysqli_connect($host,$db_user,$db_pass,$database) or die ("Failed to connect");
mysqli_select_db($database) ;
*/

session_start();

//GATHERING DATA FROM USER FORM
$email=$_POST["login"];
$pass=$_POST["pwd"];

//COMMANDING WHERE TO FIND MATCH FOR LGOIN INFORMATION
$veryfy="SELECT * FROM users WHERE Email='$email' Password='$pass'";
$query=mysql_query($veryfy) or die ( mysql_error() );

$match=0;
$match=mysql_num_rows($query);

//IF MATCH FOUND THEN SETTING SESSION AND REDIRECTING IT TO LOGGED PAGE
if($match==1)
{
$_SESSION['loggedin'] = "true";
header ("Location: logged.php"); //REDIRECTING USER TO ITS HOMEPAGE
}
else //IF MATCH NOT FOUND THEN REDIRECTING IT BACK TO LOGIN PAGE
{
$_SESSION['loggedin'] = "false";
header ("Location: index.php");
}
//PERSONAL COMMENTS OR DETIALED COMMENTS
//PROBLEM WITH THIS SCRIPT GIVING OUTPUT ON LOGIN "NO DATABASE SELECTED"
//REFRENCE from http://www.dreamincode.net/forums/topic/52783-basic-login-script-with-php/

?>

最佳答案

您正在使用 mysqli 初始化与数据库的连接。然后您尝试使用 mysql 进行查询。显然,没有通过该库与数据库建立连接,因此它因错误而失败。将 mysql_query 更改为 mysqli_query

一般说明

您当前的代码容易受到 sql 注入(inject)攻击,因为您在将用户输入放入查询之前没有对其进行清理。考虑使用 prepared queries .

关于php - 错误 :NO database is selected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18940753/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com