2011-06-07 161 views
0

可能重複:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in selectMysql數據庫連接錯誤

這是我的代碼,我正在通過對象和類連接數據庫。

$this->db_connection($app_config['database']); 
$this->table_name = $app_config['database']['tbl_prefix'].strtolower(get_class($this)); 
$column_q = $this->query("SHOW COLUMNS FROM {$this->table_name}"); 
while($rows = mysql_fetch_array($column_q)){ #error showing in this line 
    $column[] = $rows['Field']; 
    if($rows['Field'] == 'PRI'){ 
    $this->primary_key = $rows['Field']; 
    } 
} 

它顯示出一些錯誤,如

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in 

可以有一個人告訴我什麼是錯誤,以及如何解決這一問題?

+0

一個生活在錯誤之上,您的查詢失敗並返回FALSE。所以var保持FALSE,這是你給mysql_fetch_array的 – Rufinus 2011-06-07 12:31:51

回答

0

也許這 - >查詢返回錯誤的查詢與錯誤?所以你的需要:

if ($column_q = $this->query("SHOW COLUMNS FROM {$this->table_name}")) 
    while($rows = mysql_fetch_array($column_q)){...} #error showing in this line 
else 
    echo "Error!" 

PS。我更喜歡簡單地返回返回$ mdb-> query()在自己的簡單的MySQL類...或顯示錯誤。

0

我的猜測是您的查詢失敗,並返回false如下:

$column_q = $this->query("SHOW COLUMNS FROM {$this->table_name}"); 

做一個var_dump($column_q)echo您的查詢,以確保其符合市場預期。