这是我的代码:
$con = mysqli_connect($databasehost,$dbname,$dbpassword) or die(mysql_error());
mysqli_select_db($con, $dbname) or die(mysql_error());
mysqli_query($con,"SET CHARACTER SET utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con,$query);
if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($row = mysql_fetch_assoc($sth)){
$rows[] = $r;
}
print json_encode($rows);
}
问题是什么?
这是错误:
[19-Jan-2018 09:17:39 UTC] PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource on line 22
答案 0 :(得分:0)
您正在混合mysqli
和mysql
。
自版本5.5.0起,后者已弃用。
尝试使用mysqli_fetch_assoc($sth)
代替mysql_fetch_assoc($sth)
答案 1 :(得分:-1)
你的问题是你正在混合mysqli和mysql。
尝试使用mysqli_fetch_assoc($sth)
代替mysql_fetch_assoc($sth)
。
而且,在mysqli_connect("host", "user", "password", "database")
中,您缺少用户名。
// (1) make sure $username is defined.
$con = mysqli_connect($databasehost,$username,$dbname,$dbpassword) or die(mysqli_error());
// check to use mysqli_, not mysql_
mysqli_select_db($con, $dbname) or die(mysqli_error());
mysqli_query($con,"SET CHARACTER SET utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con,$query);
if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
// (2) Use mysqli_fetch_assoc instead of mysql_fetch_assoc.
while($row = mysqli_fetch_assoc($sth)) {
// (3) $r is not defined. Use $row.
$rows[] = $row;
}
print json_encode($rows);
}