2011-12-21 61 views
0

Os我开始使用mysqli,我对它的工作原理有些困惑。mysqli fetch_object()抛出一个错误

所以我有一个功能:

function verify_payment_date() 
{ 
    $today = date("Y-m-d"); 

    $email = $_SESSION['email']; 

    $result = $this->conn->query("SELECT * FROM user WHERE email=$email"); 

    while ($row = $result->fetch_object()) 
    { 
     $next_payment_date = $row['next_payment_date']; 
    } 
} 

要设置我的连接我在同一个班级做到这一点:

private $conn; 

function __construct() 
{ 
    $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or 
        die('There was a problem connecting to the database.'); 
} 

谁能给我伸出援助之手在这里,因为我完全丢失。我在一些基础的代码本网站: http://www.willfitch.com/mysqli-tutorial.html

而且我得到的错误是:

致命错误:调用一个成员函数取()一个非对象在/ home/vhosts/tradingeliteclub.com/subdomains/test/httpdocs/FES/members/classes/Mysql.php on line 52

我不知道该从哪里出发。

感谢您的时间和帮助。

+1

欢迎来到Stack Overflow!您在查询中没有执行任何错误检查,因此无法在代码失败时中断代码。在每个'mysql_query()'调用之后做适当的检查。否则,如果查询失败,脚本将中断。如何做到这一点在[mysql_query()'](http://php.net/mysql_query)手册或本[参考问题。](http://stackoverflow.com/questions/6198104/reference -what-a-perfect-code-sample-using-the-mysql-extension) – 2011-12-21 16:41:07

+0

把电子邮件地址放在引号中:'$ this-> conn-> query(“SELECT * FROM user WHERE email ='$ email ““);'。 – 2011-12-21 16:44:22

回答

1

$result不是mysql结果,您的查询失败。尝试

$result = $this->conn->query("SELECT * FROM user WHERE email='$email'"); 
// apostrophes 

改为。

$this->conn->query返回resource成功,false失败。你可以这样避免错误

if($result) while ($row = $result->fetch_object()) 
{ 
    $next_payment_date = $row['next_payment_date']; 
}