【问题标题】:PHP Fatal error: Call to a member function fetch_assoc() on a non-objectPHP 致命错误:在非对象上调用成员函数 fetch_assoc()
【发布时间】: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-&gt;error); }.
  • 2 小时我查看相关问题并使用 Google 搜索并尝试所有变体。它不起作用。添加了错误检查 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 4 行的“-1”附近使用正确的语法
  • 听起来$limit$offset-1,这是不允许的。
  • 请把所有变量都替换后的SQL查询字符串的内容贴出来。
  • 这不可能。错误消息显示near '-1',但该查询中没有-1

标签: php select mysqli


【解决方案1】:

试试这个:

$sql = "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");
if ($limit > 0 && $offset >= 0) {
    $sql .= " LIMIT $limit OFFSET $offset";
}
$select = $mysqli->query($sql) or die ($mysqli->error);

这应该防止为LIMITOFFSET 指定负值。

【讨论】:

  • 嗯!当 offset = 0 时,将其设置为 -1,当 = 1 或 100 时,它的工作正常。谢谢!
  • 我不明白。 0-1 没有任何变化。
  • 因为我在另一行有错误 - if (empty($offset)) $offset = '-1';所以,0 = 空 谢谢!你的回答帮助我找到问题。你看到了我的生活:)
【解决方案2】:

导致您的函数看不到 mysqli 对象。您应该在函数中创建对象(连接)或作为参数传递

function skype_users($api_id,$filter,$limit,$offset,$my_country,$my_city)
{
   $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

   $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");

将 mysqli 对象作为参数传递给函数:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

function skype_users($api_id,$filter,$limit,$offset,$my_country,$my_city,$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");

   ....
}

【讨论】:

  • 他的函数中有global $mysqli。如果问题出在$mysqli,则错误来自query,而不是fetch_assoc
猜你喜欢
  • 2016-08-30
  • 2012-09-26
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 2016-02-12
  • 2016-02-09
  • 1970-01-01
相关资源
最近更新 更多