gpt4 book ai didi

php - 出现错误数据库未选择

转载 作者:行者123 更新时间:2023-11-29 14:14:08 25 4
gpt4 key购买 nike

代码:

<?php
session_start();
$_SESSION['msg'] = "";
$con = mysql_connect('localhost','me','omglol');
mysql_select_db('test',$con);
$q = mysql_query(sprintf("select * from UserTable where (nick=\"%s\") AND (pass=SHA1(\"%s\"))",$_POST['nick'],$_POST['pass']),$con) or die(mysql_error());

这对我来说看起来很正确。是的,我知道“测试”的存在。并包含UserTable。

首先,感谢rid将php4添加到我忘记的标签中:(

根据 Laser_wizard 的建议,我执行了以下操作:(整个代码):

<?php
session_start();
$_SESSION['msg'] = "";
$con = mysql_connect('localhost','me','omglol');
if(!$con)
{
die("The connection to mysql server is not being made.");
}
$db = 'test';
$selected = mysql_select_db($db,$con);
if(!$selected)
{
die(sprintf("Cannot use database %s.",$db));
}
//$q = mysql_query(sprintf("select * from UserTable where (nick=\"%s\") AND (pass=SHA1(\"%s\"))",$_POST['nick'],$_POST['pass']),$con) or die(mysql_error());
$q = mysql_query("select * from UserTable",$con) or die("The query statement still isn't working");
$row = mysql_fetch_assoc($q);
$dest=0;
if(mysql_num_rows($q)==0)
{
//$testn = mysql_query(sprintf("select * from UserTable where nick=(\"%s\")",$_POST['nick']),$con);
$testn = mysql_query("select * from Category",$con) or die("The 2nd query statement still isn't working");
if(mysql_num_rows($testn)==0)
{
$_SESSION['msg'] = "Nick ".$_POST['nick']." was not found. Check spelling or <a href=\\\"register.php\\\">register</a>";
}
else
{
$_SESSION['msg'] = "Password incorrect";
}
if(isset($_SESSION['attempts']))
{
$_SESSION['attempts'] = $_SESSION['attempts'] + 1;
}
else
{
$_SESSION['attempts'] = 1;
}
mysql_free_result($q);
mysql_free_result($testn);
mysql_close($con);
$dest = 'Location:http://cs4.sunyocc.edu/~me/onestopshop/login.php';
}
else
{
$_SESSION['nick'] = $_POST['nick'];
$_SESSION['email'] = $row['email'];
mysql_free_result($q);
mysql_close($con);
$dest = 'Location:http://cs4.sunyocc.edu/~me/onestopshop/index.php';
}
header($dest);
exit();
?>

与上面相同的错误。所以 $con 被设置并且 $selected 读取 true,所以我很困惑接下来要检查什么。我猜 mysql_select_db($db,$con); $testn 也不是仍然无法工作但仍然读取 true 吗?我很困惑下一步该做什么。

最佳答案

添加一些 die 语句来测试连接并确保其正在设置。除此之外,我想说注释掉您的查询行,看看这是否会导致问题。

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}

关于php - 出现错误数据库未选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12967295/

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