返回行数组

时间:2014-08-23 13:52:00

标签: php mysqli

我有一个我正在查询的数据库:"select email from users where access=1"。我收到的错误是

  

致命错误:在线上的非对象上调用成员函数query():$result = $mysqli->query($query);

我正在使用http://uk3.php.net/manual/en/mysqli.query.php作为参考,我无法看到我在哪里查询过程和面向对象的PHP。

<?php

$db_host = "";
$db_user = "";
$db_pass = "";
$db_name = "";

/* OOP MYSQLI DATABASE CONNECTION */
$db = new mysqli($db_host, $db_user, $db_pass, $db_name);

if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

/*LOGIN QUERY */
$query = "select email from users where access=1";
$result = $mysqli->query($query);

$email= $result->fetch_array(MYSQLI_NUM);    
if(isset($email[0])){    
    $query1="select id from user where email='".$email[0]."' and facebook=1";
    $result1 = $mysqli->query($query1);
    $email1= $result1->fetch_array(MYSQLI_NUM);  
    $userId=$admin[0];
}
?>

有人会介意解释吗?

1 个答案:

答案 0 :(得分:6)

您的MySQLi对象未命名为$mysqli,其名称为$db

$result = $mysqli->query($query);

应该是

$result = $db->query($query);

同样适用于

if ($mysqli->connect_error) {

应该是

if ($db->connect_error) {

修改

只是旁注,你的第二个查询根本没有任何意义:)你只是从该表中提取了一封电子邮件,然后你再次查询该电子邮件的同一个表?

$email= $result->fetch_array(MYSQLI_NUM);    
if(isset($email[0])){    
$query1="select id from user where email='".$email[0]."' and facebook=1";

所有设置都等同于您的第一个查询

select id,email from users where access=1 and facebook=1

然后你不需要第二个查询