I'm trying to display rows from a mysql database in a table but I can't get the PHP to display in the html. This is my code:
<?php
$connection = mysql_connect("localhost","root","")
or die("no connection");
$db_select=mysql_select_db("smqr",$connection)
or die("no connection to db");
$query= ("SELECT * FROM seafood");
$result=mysql_query($query)or die(mysql_error());
while
($row =mysql_fetch_array($result)):
$recipe=$row['recipe'];
$usrtext=$row['usrtext'];
$usrtxt=$row['usrtxt'];
endwhile;
?>
<body bgcolor="#ffccff">
<table align="center" width="780" height="100%" bgcolor="lightgrey" border="1">
<thead>
</thead>
<tbody>
<tr>
<th height="220">
<img src="seafoods.jpg" width="100%" height="220" /></th></tr>
<tr>
<th>Recipe Name <p>
<? echo "$recipe" ?></p></th></tr>
<tr>
<th>Ingrediants and Measurements
<p><? echo $usrtext ?></p></th></tr>
<tr>
<th>Instructions
<p> <? echo $usrtxt ?></p></th>
</tr>
I thought I had to echo the table in the while loop and I tried echo table but it didn't work so I tried to add PHP in the html which is what I have posted. When I echo $recipe
before endwhile it will display the info but I need it in the table.
Check if php short tags is enabled in your php settings file. if not you can't use <?
you must use <?php
example:
<?php echo $usrtext ?>
Also The while loop ended before the display table. end the while loop after display that is at the end.
try this
<table align="center" width="780" height="100%" bgcolor="lightgrey" border="1">
<tr>
<td>Recipe Name </td>
<td>Ingrediants and Measurements</td>
<td>Instructions</td>
</tr>
<?php
$connection = mysql_connect("localhost","root","")or die("no connection");
$db_select=mysql_select_db("smqr",$connection) or die("no connection to db");
$query= ("SELECT * FROM seafood");
$result=mysql_query($query)or die(mysql_error());
while($row=mysql_fetch_array($result))
{
$recipe=$row['recipe'];
$usrtext=$row['usrtext'];
$usrtxt=$row['usrtxt'];
?>
<tr>
<td><?=$recipe?></td>
<td><?=$usrtext?></td>
<td><?=$usrtxt?></td>
</tr>
<?php
}// End while
?>
</table>
Rewrite your code like this by adding
<?php ?> instead of <? ?>
<?php
$connection = mysql_connect("localhost","root","")
or die("no connection");
$db_select=mysql_select_db("test",$connection)
or die("no connection to db");
$query= ("SELECT * FROM profile");
$result=mysql_query($query)or die(mysql_error());
while
($row =mysql_fetch_array($result)):
$recipe=$row['recipe'];
$usrtext=$row['usrtext'];
$usrtxt=$row['usrtxt'];
endwhile;
?>
<body bgcolor="#ffccff">
<table align="center" width="780" height="100%" bgcolor="lightgrey" border="1">
<thead>
</thead>
<tbody>
<tr>
<th height="220">
<img src="seafoods.jpg" width="100%" height="220" /></th></tr>
<tr>
<th>Recipe Name <p>
<?php echo "$recipe" ?></p></th></tr>
<tr>
<th>Ingrediants and Measurements
<p><?php echo $usrtext ?></p></th></tr>
<tr>
<th>Instructions
<p> <?php echo $usrtxt ?></p></th>
</tr>
$recipe, $usrtext and $usrtxt are all defined within the local scope of your while loop so they won't exist after "endwhile;" anymore
<?
//define variables outside the while loop
$recipe;
$usrtext;
$usrtxt;
while($row =mysql_fetch_array($result)):
$recipe=$row['recipe'];
$usrtext=$row['usrtext'];
$usrtxt=$row['usrtxt'];
endwhile;
?>
<body bgcolor="#ffccff">
<table align="center" width="780" height="100%" bgcolor="lightgrey" border="1">
<thead>
</thead>
<tbody>
<tr>
<th height="220">
<img src="seafoods.jpg" width="100%" height="220" />
</th>
</tr>
<tr>
<th>
Recipe Name
<p> <? echo $recipe ?></p>
</th>
</tr>
<tr>
<th>
Ingrediants and Measurements
<p><? echo $usrtext ?></p>
</th>
</tr>
<tr>
<th>
Instructions
<p> <? echo $usrtxt ?></p>
</th>
</tr>
</table>
</body>
You code contains some bad stuff, first of all, you shouldn't use <? ?>
<? ?>
, I don't even know if that's valid somewhere. Use either <?= ?>
or <?php echo ?>
(but use <?php
instead, because it's more compatible.
Secondary, it would be a good idea to use PDO library istead of MySQL, it's safer, easyer, more flexible and more compatible, use this tutorial.
Now, you can try this:
<?php
$connection = mysql_connect("localhost","root","")
or die("no connection");
$db_select=mysql_select_db("smqr",$connection)
or die("no connection to db");
$query= ("SELECT * FROM seafood");
$result=mysql_query($query)or die(mysql_error());
?>
<table>
<?php
while($row=mysql_fetch_array($result)): // everything between "while():" and "endwhile;" will be outputted in a cycle
?>
<tr>
<td>
<?php echo $row['recipe']; ?>
</td>
<td>
<?php echo $row['usrtext']; ?>
</td>
<td>
<?php echo $row['usrtxt']; ?>
</td>
</tr>
<?php endwhile; ?>
</table>
this is called inline coding
You can do it like this.
...
while($row = mysql_fetch_array($result)) {
?>
<table align="center" width="780" height="100%" bgcolor="lightgrey" border="1">
<thead>
</thead>
<tbody>
<tr>
<th height="220">
<img src="seafoods.jpg" width="100%" height="220" />
</th>
</tr>
<tr>
<th>Recipe Name <p>
<?php echo $row['recipe']; ?></p>
</th>
</tr>
<tr>
<th>Ingrediants and Measurements
<p><?php echo $row['usrtxt']; ?></p>
</th>
</tr>
<tr>
<th>Instructions
<p><?php echo $row['usrtxt']; ?></p>
</th>
</tr>
</tbody>
</table>
<?php } ?>
If your query result returns more than one row , then most probably you are looking for something like this:
<?php
$connection = mysql_connect("localhost", "root", "") or die("no connection");
$db_select = mysql_select_db("smqr", $connection) or die("no connection to db");
$query = "SELECT * FROM seafood";
$result = mysql_query($query) or die(mysql_error());
?>
<body bgcolor="#ffccff">
<table align="center" width="780" bgcolor="lightgrey" border="1">
<thead>
<tr>
<th>Recipe Name</th>
<th>Ingrediants and Measurements</th>
<th>Instructions</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysql_fetch_array($result)): ?>
<tr>
<td><?php echo $row['recipe']; ?></td>
<td><?php echo $row['usrtext']; ?></td>
<td><?php echo $row['usrtxt']; ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
Note: If the above code works for you, carefully follow what changes I made on your code. Hope this will help you learn writing better code. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.