当我执行代码时,这个错误返回给我
警告:PDOStatement::fetchAll() [pdostatement.fetchall]: SQLSTATE[HY000]: 一般错误:第 28 行 C:\AppServ\www\cms_project\lib\database_model.class.php 中的额外参数
警告:array_shift() [function.array-shift]:参数应该是第 34 行 C:\AppServ\www\cms_project\lib\database_model.class.php 中的一个数组
警告:第 43 行 C:\AppServ\www\cms_project\lib\cat.class.php 中为 foreach() 提供的参数无效
这是 database_model 类:
<?php
class database_model
{
public function attribute()
{
$string = array();
foreach($this->db_feild as $feild){
if(is_int($this->$feild) || is_double($this->$feild)){
$string[] = $feild . " = ".$this->$feild."";
}else{
$string[] = $feild ." = '".$this->$feild ."'";
}
}
return implode(', ',$string); // convert array to string
}
public static function read($sql, $type = PDO::FETCH_ASSOC,$class = null)
{
global $db_connect;
$resualt= $db_connect->query($sql); // query from db
if($resualt){
if(null !== $class && $type == PDO::FETCH_ASSOC){
$data = $resualt->fetchAll($type,$class);
}else{
$data = $resualt->fetchAll($type);
}
if(count($data)== 1){
$data = array_shift($data); // array_shift علشان نطلع من الارارى
}
return $data;
}else{
return false;
}
}
private function add()
{
global $db_connect; // to use the conection to database
$sql = "INSERT INTO ".$this->table_name . " SET " .$this->attribute();
$affectedrows = $db_connect->exec($sql); // to include to database
if($affectedrows != false){
$this->id = $db_connect->lastInsertId();
}else{
return false;
}
return true;
}
private function update()
{
global $db_connect;
$sql = "UPDATE ".$this->table_name . " SET " .$this->attribute()
."WHERE id = ".$this->id;
$affectedrows = $db_connect->exec($sql);
return $affectedrows != false ? true : false;
}
public function delete()
{
global $db_connect;
$sql = "DELETE FROM ".$this->table_name."WHERE id = ".$this->id;
$affectedrows = $db_connect->exec($sql);
return $affectedrows != false ? true : false;
}
public function save()
{
return ($this->id === null)? $this->add():$this->update();
}
}
?>
这是猫类:
class cat extends database_model
{
public $id;
public $user_id;
public $created;
public $title;
public $slug;
public $content;
public $table_name = 'cat';
public $db_feild = array(
'user_id',
'created',
'title',
'slug',
'content',
);
public static function control()
{
// __CLASS__ ==> RETURN THE THIS CLASS
$all_categries = self::read("SELECT * FROM cat ",PDO::FETCH_ASSOC,__CLASS__);
$table = '<table class="cpanel_cat">';
$table .= '<tr>
<th># </th>
<th>category name </th>
<th colspan="2"> control </th>
' ;
if($all_categries !== false){
if(is_object($all_categries)){
$table .= '<tr>
<td>1 </td>
<td>'. $all_categries->title .'</td>
<td><a href="" >EDIT</a> | <a href="" > Delete</a> </td>
';
}else{
$i = 1;
foreach($all_categries as $cat)
$table .= '<tr>
<td>'.$i++ .' </td>
<td>'.$cat->title .'</td>
<td><a href="" >EDIT</a> | <a href="" > Delete</a> </td>
';
}
}else{
$table .= '<tr><td colspan="4">sorry categroy not found !!</td></tr>';
}
$table .='</table>';
return $table;
}
}