我想创建一个PDO类和我得到的错误:PHP PDO类不能运行执行
PHP致命错误:调用一个成员函数执行()非物体上线39
调用这个类我创建的,没有任何问题连接一个新的数据库对象,然后我这样做:
$newDailyTotal = array(
array('date',time()),
array('cash',300)
);
$db->insert('dailyTotals',$newDailyTotal);
它回声的INSERT INTO dailyTotals(日期,现金)值
所以(?,?)那看起来 没关系。 感谢您的所有帮助。
<?
类数据库{
private $DBH;
//connects to the database
function __construct($host,$dbname,$user,$pass) {
try {
$this->DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
//inserts into the database
//$tableName name of the table to insert the info into
//$items is a multidimensional array of array(column name, value)
public function insert($tableName,$items){
$values = array();
$sql = "INSERT INTO $tableName(";
$valuePlaceHolder = ''; // holds the question marks at the end of the PDO sql string
foreach($items as $item){
$sql .= $item[0] . ',';
array_push($values, $item[1]);
$valuePlaceHolder .= '?,';
}
// remove the last comma from the sql statement
$sql = substr($sql,0,-1);
$valuePlaceHolder = substr($valuePlaceHolder, 0, -1);
$sql .= ") values ($valuePlaceHolder)";
echo $sql;
$SHT = $this->DBH->prepare($sql);
$STH->execute($values);
}
}
?>
呃..是的,是的,我有。我比任何程序员都要努力挣扎的多。 – Avovk 2012-02-17 20:40:45