我想創建一個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