Jump to content

[SOLVED] Using PHP to pull data from MySQL and put into HTML - Novice


ofmyst

Recommended Posts

I have a mysql database called paintings with a table called pieces consisting of data (Title, Medium, Size). 

I have a PHP file (tinker2.php) that creates a table consisting of the information pulled from the mySQL database.

 

My question:

 

How can I have the table show up in my html file? 

 

Or am I going about this improperly?

 

I am completely new to PHP and MySQL.

 

Thanks so much,

 

Sharon

 

 

The PHP file creates a HTML table, so if I go to tinker2.php the result is an HTML table filled in from mysql. My problem is that I want that table to show up in a different HTML file that has other information in it.  The data table will be just part of the full page.   I hope that is more clear.  Unfortunately, I do not know the proper language for what I am trying to do.

Ok, I'm not quite sure what it is you want, but the following code will display the results of a query in an html table. Of course you'll have to change the query etc to suit your needs.....

 

// Get all the data from the "example" table


$result = mysql_query("SELECT * FROM example")
or die(mysql_error());

echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";


// keeps getting the next row until there are no more to get


while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td></tr>";
}

echo "</table>";

Thank you, that worked.  My next related question is, using your example:

 

If rather than the table looking like this:

 

Name  Age

Name  Age

Name  Age 

 

I wanted it to look like this:

 

Name    Name    Name

Age      Age      Age

 

How would I write the code?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.