2012-08-15 93 views
1

我正在做一個後臺項目 - 我第一次實現了添加,更新和刪除數據庫條目,現在我需要在某些數據類型上實施一些特定的操作。 所以我想檢索數據類型 只有$ fieldtypes = mysql_query(「SHOW FIELDS FROM mysqltable」);返回NULLSHOWFIELD從mysqltable沒有結果

這是代碼是如何在這個時候:

<?php 
$serveur='localhost'; 
$user='root'; 
$password='xxxx'; 
$base='db'; 
$champs=array(
"member"=>array("id","group","login","lastname","firstname","email","pswd","account","searchingfor","searchingfordistance","searchedfor","searchedfordistance","mydescription","groupdescription","searchdescription","resourcesdescription"), 
    "place"=>array("id","idm","ids","name","town","postalcode","address","coord") 
); 

$connexion = mysql_connect("$serveur","$user","$password") or die ("Impossible de se connecter à la base de données"); 
mysql_select_db("$base",$connexion) or die("Erreur de connexion a la base de donnees"); 

$fieldtypes = mysql_query("SHOW FIELDS FROM place"); 

ob_start(); 
var_export($fieldtypes); 
$tab_debug=ob_get_contents(); 
ob_end_clean(); 
$fichier=fopen('gs.log','w'); 
fwrite($fichier,$tab_debug); 
fclose($fichier); 

... (rest of code works) 

誰能幫我找出什麼地方出了錯?

謝謝!

回答

1

即使使用像SHOW FIELDS這樣的元數據查詢,仍需要從結果資源中提取行。它的行爲就像是一個返回行的常規查詢,所以像你一樣會在while循環中獲取它們。

$fields = array(); 
$fieldtypes = mysql_query("SHOW FIELDS FROM place"); 
if ($fieldtypes) { 
    while ($row = mysql_fetch_assoc($fieldtypes)) { 
    $fields[] = $row; 
    } 
} 
ob_start(); 
// Dump your $fields array 
var_export($fields); 
$tab_debug=ob_get_contents(); 
ob_end_clean(); 

順便說一句,這是不必要的,多餘的包圍在雙引號中的變量時,他們並沒有受到一個字符串內插值:

// Don't quote these vars -- it's poor practice 
$connexion = mysql_connect("$serveur","$user","$password") 
+0

哦,是的 - 它的工作原理 - 非常感謝你:) – 2012-08-15 04:28:49