简体   繁体   English

用PHP显示MySQL表

[英]Displaying mysql table with php

I'm having trouble displaying my mysql table using php code. 我在使用php代码显示mysql表时遇到问题。 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. 我知道我的用户名密码和db都是正确的,但是就像我说的那样,表格未显示我添加的值。 Any help would be much appreciated This is my mysql code: 任何帮助将不胜感激这是我的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);
?>

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. 注意:由于您要为不同的型号添加相同的品牌名称,因此不能将品牌作为主键。

在此输入图像描述

I don't see why you are using the fetch_field call. 我不明白您为什么使用fetch_field调用。 I'm assuming that you know ahead of time what the actual names of each field in your table is prior to 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. 我认为为简单起见(较少的循环和嵌套循环),您应该手动编写字段名称,然后循环遍历输入值的数据。

$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 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. 当您完成显示标题时,查询结果的内部指针将到达最后一行,因此您的mysql_fetch_row()调用失败,因为没有更多的行可提取。 Call mysql_data_seek(0); 调用mysql_data_seek(0); before printing the table rows, to move the internal pointer back to the first row. 在打印表行之前,请将内部指针移回第一行。

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>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM