2016-08-31 3 views
-3

私はPHPの初心者です& MySQLと私はPHPについて& MySQLについて練習しています。フィールド名だけが正しいが、それ以外は有線で接続されている。私はコードから期待される結果を得ることができません。 データベースから正しいメタデータを取得するにはどうすればよいですか? The expected resultPHPとMySQLについての奇妙なデータ出力

Actual result

<?php 
    // Open a connection to the server and USE the winestore 
     $link = mysqli_connect("localhost","root","","winestore"); 

     /* check connection */ 
     if (mysqli_connect_errno()) 
     { 
      printf("Connect failed: %s\n", mysqli_connect_error()); 
      exit(); 
     } 

     // Run a query on the wine table in the winestore database to retrieve 
     // one row 
     $query = "SELECT * FROM wine LIMIT 1"; 
     $result = mysqli_query($link, $query); 

     // Output a header, with headers spaced by padding 
     print "\n" . 
      str_pad("Field", 20) . 
      str_pad("Type", 14) . 
      str_pad("Null", 6) . 
      str_pad("Key", 5) . 
      str_pad("Extra", 12) . "\n"; 

     // How many attributes are there? 
     $x = mysqli_num_fields($result); 

     // for each of the attributes in the result set 
     for($y=0;$y<$x;$y++) 
     { 
      // Get the meta-data for the attribute 
      $info = mysqli_fetch_field ($result); 

      // Print the attribute name 
      print str_pad($info->name, 20); 

      // Print the data type 
      print str_pad($info->type, 6); 

      // Print the field length in brackets e.g.(2) 
      print str_pad("({$info->max_length})", 8); 

      // Print out YES if attribute can be NULL 
      if ($info->not_null != 1) 
       print " YES "; 
      else 
       print "  "; 

      // Print out selected index information 
      if ($info->primary_key == 1) 
      print " PRI "; 
      elseif ($info->multiple_key == 1) 
      print " MUL "; 
      elseif ($info->unique_key == 1) 
      print " UNI "; 

      // If zero-filled, print this 
      if ($info->zerofill) 
      print " Zero filled"; 

      // Start a new line 
      print "\n"; 
     } 
     mysqli_free_result($result); 
     mysqli_close($link); 
    ?> 
+0

'str_pad'をご覧ください!なぜテーブルだけではないのですか? – Ghost

+0

繰り返しごとに '$ info'をチェックして何が起きているのかを確認してください – Ghost

答えて

0

期待される結果は、テーブル構造の詳細であるとMySQL テーブルの構造を提供するテーブルクエリを記述する1つが使用できるため、クエリを1として。

<?php 
    $con = mysqli_connect(HOST,USER,PASSWORD,DATABASE NAME); 
    $data = mysqli_query($con,"DESCRIBE tablename"); 
?> 
<table> 
    <tr> 
     <td>Field</td> 
     <td>Type</td> 
     <td>Null</td> 
     <td>Key</td> 
     <td>Default</td> 
     <td>Extra</td> 
    </tr> 
<?php while($row=mysqli_fetch_array($data,MYSQLI_ASSOC)){ ?> 
    <tr> 
     <td><?php echo $row['Field']; ?></td> 
     <td><?php echo $row['Type']; ?></td> 
     <td><?php echo $row['Null']; ?></td> 
     <td><?php echo $row['Key']; ?></td> 
     <td><?php echo $row['Default']; ?></td> 
     <td><?php echo $row['Extra']; ?></td> 
    </tr> 
<?php } ?> 
</table> 
+0

あなたのコードにも説明を加えてください。 –

+0

クエリごとに、期待される結果はテーブル構造の詳細であり、そのためには** mysqlを使用することができます。表の**構造**を提供するテーブルクエリ**を記述します。 – PBajpai

+0

あなたの答えを編集し、この情報を回答に追加してください。人々はコメントを読むことはできません。 :) –

0

あなたは次のコードを試してみることができます。

<?php 
error_reporting(0); 
$conn = mysqli_connect('localhost', 'root', ''); 
if (!$conn) { 
die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db('winestore'); 
$result = mysql_query('SELECT * FROM wine LIMIT 0,1'); 

if (!$result) { 
die('Query failed: ' . mysql_error()); 
} 
?> 

<table> 
<th>Field</th> 
<th>Type</th> 
<th>Null</th> 
<th>Key</th> 
<th>Extra</th> 
<?php 
/* get column metadata */ 
$i = 0; 
while ($i < mysql_num_fields($result)) { 
$meta = mysql_fetch_field($result, $i); 
if (!$meta) { 
    echo "No information available<br />\n"; 
} 
$i++; 
?> 
<tr> 
    <td><?php echo $meta->name;?></td> 
    <td><?php echo $meta->type."(".$meta->max_length.")";?></td> 
    <td><?php if($meta->not_null==1){echo 'NO';}else{echo 'YES';}?></td> 
    <td><?php if($meta->multiple_key==1){ echo 'MUL';}if($meta->primary_key==1){echo 'PRI';}?></td> 
    <td><?php echo $meta->not_null;?></td> 
</tr> 
<?php 
} 
mysql_free_result($result); 
?> 

あなたはこの問題についてのより多くの疑問を持っている場合は、このリンクhttp://php.net/manual/en/function.mysql-fetch-field.php