Jump to content

Putting information into a table?


mcc_22ri

Recommended Posts

Hi Everyone,

 

I'm trying to figure out a way to put my MySql data into a table. I'm close to getting this completed by I'm having some trouble. The data I'm inserting isn't being put into the correct tables. I'm not quite sure what I'm doing wrong. Any help?

 

http://whatsmyowncarworth.com/practice/table.php

 

<?php
include('init.php');


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


echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Last Name</th> <th>Address</th> <th>City</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['first_name'];
echo "</td><td>"; 
echo $row['last_name'];
echo "</td></tr>"; 

echo "<tr><td>"; 
echo $row['address'];
echo "</td><td>"; 
echo $row['city'];
echo "</td></tr>"; 
} 

echo "</table>";
?>

Link to comment
Share on other sites

In addition, I find it's easier to close your PHP portion and write the HTML so it's more legible than echo'ing each instance of HTML markup:

 

<?php
include('init.php');

$sql = "SELECT * FROM `info`";
if ($result = mysql_query($sql)) {

echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Last Name</th> <th>Address</th> <th>City</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
	?>
	<tr>
		<td><?php echo $row['first_name']; ?></td>
		<td><?php echo $row['last_name']; ?></td> 
		<td><?php echo $row['address']; ?></td> 
		<td><?php echo $row['city']; ?></td>
	</tr>
	<?php
}
echo "</table>";
}
else {
trigger_error(mysql_error()); // for development only; remove when in production
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.