2

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);
?>
1
  • Can you check your table from mysql/phpmyadmin ? Commented Apr 18, 2012 at 1:02

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

7 Comments

I understand what this code is doing however it yields the same result as my previous code. It displays a table that only says Brand and Model and not the values
Have you ensured the table has values prior to calling this function?
i made a home work with this and i got result too. Please see update
Ramesh is that screenshot the output of the code that I have except you replaced my while loop with yours? Or did you just add that while loop because I'm still getting an empty table
@rsay3 just used the code what you had i haven't changed anything. There is no problem with your code. Please check your table.
|
3

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;

Comments

0

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.

1 Comment

Do I have to call mysql_data_seek(0) before the while loop then?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.