当我执行代码
时,这个错误会返回给我警告: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;
}
}
答案 0 :(得分:0)
错误General error: Extraneous additional parameters
来自以下几行:
$type = PDO::FETCH_ASSOC
$class = __CLASS__
$data = $resualt->fetchAll($type,$class);
如果您尝试通过传递类名从数据库中获取记录作为类:
$type
应为PDO::FETCH_CLASS
而不是PDO::FETCH_ASSOC
当fetchAll()
失败时,它不会返回导致array_shift()
和foreach()
失败的数组。