我收到以下错误消息:Fatal error: Call to a member function prepare() on a non-object in <file location>
代码:
$obj = new MySQLi();
$mysqli = $obj->connect();
/* Create a prepared statement */
if($stmt = $mysqli -> prepare(...)) { <--- error line
....more code
}
class MySQLi {
public $mysqli;
public function connect() {
$this->mysqli = new mysqli('localhost', 'root', '', 'icu');
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
mysqli_set_charset($this->mysqli, "utf8");
return $this->mysqli;
}
public function con_close(){
mysqli_close($con);
}
}
答案 0 :(得分:1)
您需要一个mysqli实例:
$mysqli = new mysqli('localhost', 'root', '', 'icu');
这里有空格->
:
if($stmt = $mysqli -> prepare(...))....
将其更改为:
if($stmt = $mysqli->prepare(...))...
答案 1 :(得分:1)
MySQLi类没有->connect()
方法,您可能正在使用重复的类名,这绝不是一个好主意(这也是您获得{{1}的原因并且无法调用NULL
方法。
在MySQLi类中,您可以在constructor:
中启动连接prepare