PHP致命错误:在非对象上调用成员函数fetch_assoc()

时间:2014-04-14 15:47:20

标签: php select mysqli

我刚刚开始重新编程我现有的函数以使用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())

2 个答案:

答案 0 :(得分: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指定负值。

答案 1 :(得分:-1)

导致你的函数看不到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");

   ....
}