【发布时间】:2014-04-14 15:47:19
【问题描述】:
我刚刚开始重新编程我现有的函数以使用 mysqli。 简单的 mysqli 请求,例如:
$select = $mysqli->query("SELECT name, id FROM countries WHERE id > 0");
while ($row = $select->fetch_assoc())
{
$country_name = $row['name'];
$country_id = $row['id'];
$items .= "<country_id>$country_id</country_id><country_name>$country_name</country_name>";
}
工作正常,但最近 2 小时我试图完成这个复杂的请求:
function skype_users($api_id,$filter,$limit,$offset,$my_country,$my_city)
{
global $mysqli;
$select = $mysqli->query("SELECT co.name AS country, ci.name AS city, us.user_id, us.user_nickname, us.user_sex, us.avatar_id, sk.skype_id, sk.comment, us.user_birthday, us.country_id, us.city_id
FROM countries co, cities ci, users us, skype_users sk
WHERE us.country_id = co.id AND us.city_id = ci.id AND us.user_id = sk.user_id AND sk.active = 1 AND us.user_sex = '$filter' AND us.country_id = '$my_country'
ORDER BY sk.id DESC LIMIT $limit OFFSET $offset");
while ($row = $select->fetch_assoc())
{
$country = $row['country'];
$city = $row['city'];
$user_id = $row['user_id'];
$user_nickname = $row['user_nickname'];
$user_sex = $row['user_sex'];
$avatar_id = $row['avatar_id'];
$user_birthday = $row['user_birthday'];
$country_id = $row['country_id'];
$city_id = $row['city_id'];
$comment = $row['comment'];
$skype_id = $row['skype_id'];
$items .= "
<country>$country</country>
<city>$city</city>
<userId>$user_id</userId>
<userNickName>$user_nickname</userNickName>
<userSex>$user_sex</userSex>
<avatarId>$avatar_id</avatarId>
<userBirthday>$user_birthday</userBirthday>
<userCountryId>$country_id</userCountryId>
<userCityId>$city_id</userCityId>
<skypeComment>$comment</skypeComment>
<skypeId>$skype_id</skypeId>";
}
return $items;
}
它向我发送了一个错误:
PHP 致命错误:在第 10 行调用非对象上的成员函数
fetch_assoc()。
第 10 行是:
while ($row = $select->fetch_assoc())
【问题讨论】:
-
我发誓这个 same 问题每天被问 3 次(请查看本页右侧的“相关”问题)。无论如何,您需要添加错误检查。您不能只是假设您的查询有效。
if($select === FALSE){ die($mysqli->error); }. -
2 小时我查看相关问题并使用 Google 搜索并尝试所有变体。它不起作用。添加了错误检查 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 4 行的“-1”附近使用正确的语法
-
听起来
$limit或$offset是-1,这是不允许的。 -
请把所有变量都替换后的SQL查询字符串的内容贴出来。
-
这不可能。错误消息显示
near '-1',但该查询中没有-1。