在SHOW FIELDS FROM mysql表中没有结果

时间:2012-08-15 01:36:04

标签: php mysql

我正在开发一个后台项目 - 我首先实现了添加,更新和删除数据库条目,现在我需要对某些数据类型进行一些特定的操作。 所以我想检索数据类型 只有$ fieldtypes = mysql_query(“SHOW FIELDS FROM mysqltable”);返回NULL

此时代码就是这样的:

<?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 个答案:

答案 0 :(得分: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")