2013-02-18 67 views
-2

这是错误说mysqli的错误非对象

Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\Grading\logging.php on line 30

我不知道写什么有IM中的mysqli还挺新的,这里是我的代码

<?php 
$username = $_POST['username']; 
$password = $_POST['password']; 
$sql  = $con->query("SELECT * FROM user WHERE username = '".$username."' "); 
$row  = $sql->fetch(); 

if ($username == $row['username'] and $password == $row['password']) 
{ 
    echo "<script>window.location.href='home.php'; </script>"; 
} 
else 
{ 
    echo "<script> alert ('Invalid Username or Password'); window.location.href='login.php'; </script>"; 
} 
?> 
+0

这是整个代码?您是否在调用函数之前在任何地方创建'$ con'对象? – Oldskool 2013-02-18 08:50:50

+0

$ con = new mysqli('localhost','root','','teacher'); – Goenitz 2013-02-18 09:02:21

+0

更好的使用预制的[mysqli wrapper - safeMysql](https://github.com/colshrapnel/safemysql/blob/master/safemysql.class.php)。它会为你节省很多头痛。 – 2013-02-18 09:13:04

回答

0

首先,你的$ con变量是什么? 通过MySQLi猜测其正确的MySQL连接。因此,您执行了MySQLi库的query()的方法。但根据documentation of that method,您可以看到执行结果为false,true或mysql_result

而且,更重要的是,mysqli_result 没有方法fetch()。 正如您在文档中看到的那样,可以使用fetch_all(),fetch_assoc()和fetch_array()函数。您可以使用它们而不是fetch()。

此外,fetch方法是类mysqli_stmt(MySQLi Statement)的方法。但是可以使用$ con> prepare()调用检索MySQLi预处理语句,而不是使用$ con> query()方法检索。

相关问题