【发布时间】:2016-07-10 11:07:14
【问题描述】:
我正在尝试通过 mysqli 以面向对象的方式连接到数据库。我有一些错误,并解决了它们,但现在我只能解决这个问题。我的代码在这里,所有名称(数据库名称、用户、密码、主机和表名称)都是正确的(实际上是复制和粘贴),但查询仍然返回 0。
<?php
class DbConnection
{
public $link;
public function __construct()
{
$this->link = new mysqli("localhost","root","","todo");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
function RegisterUsers($username, $password, $ip, $name, $email)
{
$stmt = $this->link->prepare("INSERT INTO users (Username, `Password`, ip, Name, Email) VALUES (?,?,?,?)");
$stmt->bind_param("sssss", $username, $password, $ip, $name, $email);
$stmt->execute();
$stmt->store_result();
$count = $stmt->num_rows;
return $count;
}
}
$dbConn = new DbConnection();
echo $dbConn->RegisterUsers("a","a","a","a", "a");
?>
编辑:使用这段代码,我得到一个
在布尔值上调用成员函数 bind_param()
错误。
【问题讨论】:
-
您是否遇到任何错误?或使用 printf("Error: %s.\n", $stmt->error);打印错误
-
是的,我收到一个错误,没有电子邮件值。现在我收到
Call to a member function bind_param() on boolean错误。我更新了代码@Naisapurushotham -
将另一个占位符添加到
bind_param。它应该看起来像这样bind_param("sssss",$username, $password, $ip, $name, $email)。每个参数一个占位符,?标记相同,您需要其中五个 -
那个?,不记得他们了...解决了!非常感谢! @AlexAndrei 你能把它贴在答案上吗?