php 用php显示mysql表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10201007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying mysql table with php
提问by Ryan Sayles
I'm having trouble displaying my mysql table using php code. All it displays is the column names not the values associated with them. I know my username password and db are all correct but like I said the table is not displaying the values I added. Any help would be much appreciated This is my mysql code:
我在使用 php 代码显示我的 mysql 表时遇到问题。它显示的只是列名,而不是与其关联的值。我知道我的用户名密码和 db 都是正确的,但就像我说的表没有显示我添加的值。任何帮助将不胜感激这是我的 mysql 代码:
CREATE TABLE Guitars
(
Brand varchar(20) NOT NULL,
Model varchar(20) NOT NULL,
PRIMARY KEY(Brand)
);
insert into Guitars values('Ibanez','RG');
insert into Guitars values('Ibanez','S');
insert into Guitars values('Gibson','Les Paul');
insert into Guitars values('Gibson','Explorer');
And this is my php code:
这是我的 php 代码:
<?php
$db_host = '*****';
$db_user = '*****';
$db_pwd = '*****';
$database = '*****';
$table = 'Guitars';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
?>
回答by GoSmash
Try this:
尝试这个:
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "</tr>\n";
}
Update:
更新:
Note: You can't make brand as primary key since you gonna add same brand name for different models.
注意:您不能将品牌作为主键,因为您要为不同型号添加相同的品牌名称。


回答by JT Smith
I don't see why you are using the fetch_fieldcall. I'm assuming that you know ahead of time what the actual names of each field in your table is priorto calling it's data? I think for simplicity sake (less loops and nested loops) you should write the name of the fields manually, then loop through the data entering the values.
我不明白你为什么要fetch_field打电话。我假设您在调用数据之前提前知道表中每个字段的实际名称是什么?我认为为了简单起见(更少的循环和嵌套循环),您应该手动编写字段的名称,然后遍历输入值的数据。
$feedback .= "<table border='1'><tr>";
$feedback .= "<th>Brand</th><th>Model</th></tr>";
while ($row = mysql_fetch_array($result)) {
$feedback .= "<tr><td>" . $row['Brand'] . "</td>";
$feedback .= "<td>" . $row['Model'] . "</td></tr>";
}
$feedback .= "</table>";
echo $feedback;
回答by Indrek
By the time you're done displaying the header, the query result's internal pointer will have reached the last row, so your mysql_fetch_row()calls fail because there are no more rows to fetch. Call mysql_data_seek(0);before printing the table rows, to move the internal pointer back to the first row.
当您完成显示标题时,查询结果的内部指针将到达最后一行,因此您的mysql_fetch_row()调用将失败,因为没有更多行要提取。mysql_data_seek(0);在打印表格行之前调用,将内部指针移回第一行。
回答by The Alpha
You can also try for fetching data
您也可以尝试获取数据
while ($fielddata = mysql_fetch_array($result))
{
echo '<tr>';
for ($i = 0; $i<$fields_num; $i++) // $fields_num already exists in your code
{
$field = mysql_fetch_field($result, $i);
echo '<td>' . $fielddata[$field->name] . '</td>';
}
echo '</tr>';
}

