可能重复:
Call to a member function query() on a non-object in query()?
我正在接受
致命错误:在尝试从我的数据库中的Account表中获取行计数时,调用非对象上的成员函数query()错误。这是代码:
$link = mysqli_connect("localhost", "Username", "Password", "Database");
if ($result = $mysqli->query($link, "SELECT * FROM Accounts WHERE Username=" . $_POST['EmailTbx'] . " AND Password=" . $_POST['PasswordTbx'] . "")){
$field_cnt = $result->field_count;
echo $field_cnt;
$result->close();
}
$mysqli->close();
答案 0 :(得分:1)
您的MySQLi对象在此$link
。不是$mysqli
。所以要么使用
$link->query()
或
mysqli_query($link, ...)
这是原始版本
答案 1 :(得分:-2)
看起来你的变量混乱了。尝试以下(不防止注射):
$link = mysqli_connect("localhost", "Username", "Password", "Database");
if ($result = $link->query("SELECT * FROM Accounts WHERE Username='{$_POST['EmailTbx']}' AND Password='{$_POST['PasswordTbx']}'")){
$field_cnt = $result->field_count;
echo $field_cnt;
$result->close();
}
$mysqli->close();
使用转义尝试以下内容:
$link = mysqli_connect("localhost", "Username", "Password", "Database");
$email = mysqli_real_escape_string($link, $_POST['EmailTbx']);
$pass = mysqli_real_escape_string($link, $_POST['PasswordTbx']);
if ($result = $link->query("SELECT * FROM Accounts WHERE Username='{$email}' AND Password='{$pass}'")){
$field_cnt = $result->field_count;
echo $field_cnt;
$result->close();
}
$mysqli->close();