doushu7588 2012-08-15 01:36
浏览 255
已采纳

在SHOW FIELDS FROM mysql表中没有结果

I'm working on a back office project - I first implemented add, update and delete database entries and now I need to imlement some specific actions on some datatypes. So I want to retrieve the datatype Only the $fieldtypes = mysql_query("SHOW FIELDS FROM mysqltable"); returns NULL

This is how code is at this time :

<?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)

Can anyone help me to find out what went wrong ?

Thanks!

  • 写回答

1条回答 默认 最新

  • dsifjgogw48491752 2012-08-15 01:39
    关注

    Even with a metadata query like SHOW FIELDS, you still need to fetch rows from your result resource. It behaves like a regular query returning rows, so fetch them in a while loop like you always would.

    $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(); 
    

    By the way, it is unnecessary and redundant to surround variables in double quotes when they are not being interpolated inside a string:

    // Don't quote these vars -- it's poor practice
    $connexion = mysql_connect("$serveur","$user","$password")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条