1

我正在做一个后台项目——我首先实现了添加、更新和删除数据库条目,现在我需要对某些数据类型实施一些特定的操作。所以我想只检索数据类型 $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)

谁能帮我找出问题所在?

谢谢!

4

1 回答 1

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")
于 2012-08-15T01:39:40.927 回答