我已将我的问题缩小到这段代码:
<?php
$mysql_host = "mysqlXX.000webhost.com";
$mysql_user = "a4935911_******";
$mysql_password = "******";
$con = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(mysql_error());
$db = mysql_select_db('a4935911_******', $con) or die(mysql_error());
$getuname = mysqli_fetch_assoc(mysqli_query($con, "SELECT username FROM users WHERE id = 4"));
$username = $getuname ['username'];
echo "Username = " .$username;
?>
我在两页上有这个。第一个工作正常并按预期输出'admin',而第二个给我两个错误:
警告:mysqli_query() 期望参数 1 为 mysqli,第 11 行 /home/a4935911/public_html/requestUser.php 中给出的资源
警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,在第 11 行的 /home/a4935911/public_html/requestUser.php 中给出 null
这如何只导致其中一个页面出现问题?有什么帮助吗?
更新
<?php
$con=mysqli_connect("mysqlXX.000webhost.com","a4935911_XXX","XXXX","a4935911_XXX");
$id = $_SESSION['uid'];
$getuname = mysqli_fetch_assoc(mysqli_query($con, "SELECT username FROM users WHERE id = $id"));
$username = $getuname ['username'];
echo "Username = " .$username;
?>
错误:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/a4935911/public_html/requestUser.php on line 4
PHP是无情的。这次我错过了什么?如您所见,我对 PHP 很陌生。