Ryan Sayles
Ryan Sayles

Reputation: 3431

Displaying mysql table with php

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:

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

Upvotes: 2

Views: 26259

Answers (4)

JT Smith
JT Smith

Reputation: 741

I don't see why you are using the fetch_field call. 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;

Upvotes: 3

GoSmash
GoSmash

Reputation: 1108

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.

enter image description here

Upvotes: 4

The Alpha
The Alpha

Reputation: 146191

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

Upvotes: 0

Indrek
Indrek

Reputation: 887

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.

Upvotes: 0

Related Questions