这是这种情况。我创建了 3 个 PHP 文件,都在同一个项目文件夹中:
- 常量.php
- Mysql.php(类)
- 索引.php
当我运行 index.php 时,我得到:
致命错误:调用 C:\wamp\www\MyBlog\MyClass\MySql.php 中非对象的成员函数 prepare() 在线 #
常量.php:
<?php
//Define constent her
define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'myblog');
mysql.php:
<?php
require_once 'constants.php';
class MySql{
private $conn;
protected $_query;
function __constructert() {
$this->conn = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME) or
die("There was a probelm connecting the database");
return $this->conn;
}
protected function _prepareQuery()
{
//her the line that problem come from beetwen the () of if :
if (!$stmt = $this->conn->prepare($this->_query)) {
trigger_error("Problem preparing query", E_USER_ERROR);
}
return $stmt;
}
protected function _dynamicBindResults($stmt){
$meta=$stmt->result_metadata();
while ($field = $meta->fetch_field()) {
print_r($field);
}
}
function query($query){
$this->_query = filter_var($query,FILTER_SANITIZE_STRING);
$stmt = $this->_preparequery();
$stmt->execute();
$results=$this->_dynamicBindResults($stmt);
}
索引.php:
<? PHP
include 'MySql.php';
$Db= new MySql();
$Db->query("select * from status");
正如我所说,当我运行 index.php 时,我得到了这个:
致命错误:在线调用 C:\wamp\www\MyBlog\MyClass\MySql.php 中非对象的成员函数 prepare()
那条线是:
if (!$stmt = $this->conn->prepare($this->_query)) {
有关更多信息,我测试了与数据库的连接,没关系。我测试了该_query
属性是否采用 SQL 语句,并且确实如此。