Reputation: 1470
First, I really hope I don't get beat down for asking this, but I'm willing to risk it in order to be pointed in the right direction so I can learn.
I have a .php script, and inside my PHP script I have written the code to connect to MySQL database.
Here's the question:
I have a table that will be used for a very simple display of food items. Currently I have:
<table width="1024" border="0" cellpadding="10" cellspacing="5" align="center">
<tr>
<td colspan="2"><font>Item Name</font></td>
<td colspan="2"><font>Item Name</font></td>
<td colspan="2"><font>Item Name</font></td>
</tr>
and it keeps going with other table row elements...
WHERE IT SAYS "ITEM NAME," HOW DO I INPUT CODE TO PULL DATABASE INFORMATION. The database name I have is "TEST" and I'd like each item name to list a different item from the "ITEM" table.
ANY HELP WOULD TRULY BE APPRECIATED.
Upvotes: 0
Views: 8141
Reputation: 416
try this:
$conn = mysql_connect('host', 'user', 'password') or die("Connection error. Details:".mysql_error());
$db_select = mysql_select_db('<your_db_name', $conn);
$sql = mysql_query("SELECT * FROM `foods`")
if($sql)
{
echo '<table>';
while ($result = mysql_fetch_assoc($query))
{
echo '<tr><td>'.$result['<field_name1>'].'</td>';
echo '<td>'.$result['<field_name2>'].'</td>';
.....
.....
echo '</tr>';
}
echo '</table>';
}
else
{
echo "no results is there";
}
note: you should know your table column name , provide the same name in field_names you can print any number of columns present in your table. thanks
Upvotes: 0
Reputation: 513
You have to iterate over the results from the database, otherwise you'll have access only to the first row (or where the array pointer is).
Your question is the basics of programming, and there are million of resources on this topic around the web. I'll just give you a small sample code, and from here you can start debugging and modifying to your taste.
//Connect to MySQL Database
$connection = mysql_connect('host', 'user', 'password') or die("Connection error. Details: ".mysql_error());
//Select the database
$db_select = mysql_select_db('dbname', $connection);
//Create the query
$query = mysql_query("SELECT * FROM `foods`") or die("Error in query. Details: ".mysql_error());
//Check if the query has results, and iterate over it
if (mysql_num_rows($query) > 0)
{
//Creates the table
echo '<table>';
//Iterate over the MySQL results and print the data
while ($result = mysql_fetch_assoc($query))
{
echo '
<tr>
<td>'.$result['item_name'].'</td>
</tr>
';
}
echo '</table>';
}
else
{
echo "Your query hasn't returned results";
}
Try this and don't forget to change the mysql host name, username, password, db name and table name.
Regards.
Upvotes: 0
Reputation: 2667
You really need to do more research on this. Here is an example of Php code that pulls data from database courtesy of w3 schools. See here http://www.w3schools.com/php/php_mysql_select.asp.
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Upvotes: 2