我想创建一个CRUD类,用于插入,更新和删除数据,在__contruct函数中,我不想调用连接,我只会指定连接名称,然后我得到这个错误:
PHP致命错误:调用成员函数prepare ()在第60行的crud.php中的非对象上
<?php
/* Start (config.php)
This part of code is only to show you that the connection name is "connexion" which is called in config.php
$connexion = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_passwd
, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES '.$db_charset.'')); //SET NAMES utf8
$connexion->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
EnD */
classDB.php
require_once('config.php');
final class crud {
public function __construct($connexionName) {
$this->connexionName = $connexionName;
}
public final function insert($tableName, $fields=array()){
$this->tableName = $tableName;
$this->fields = $fields;
foreach ($this->fields as $kf => $vf) {
$inKeys[] = $kf;
$inKeysDotted[] = ':' . $kf;
$insertedKeys = implode(', ', $inKeys);
$insertedKeysDotted = implode(', ', $inKeysDotted);
}
echo '<br />';
$sql = "INSERT INTO $this->tableName ($insertedKeys) VALUES ($insertedKeysDotted)";
$insertItems = $this->connexionName->prepare("$sql"); // THIS IS LINE 60
echo 'insert '.$insertItems.'<br />';
} // end insert()
} // end CRUD
$con = new crud($connexion);
echo '<br />';
$con->insert('ban_ip', array('visitor_type'=>'5', 'ip'=>'111.222.333.444'));
?>
提前致谢