I’m trying to embed some HTML into a PHP file to display the fields returned
from my query in a table structure. I’m new to PHP and the error messages
are not very user friendly, and I’m not sure what I’m doing wrong. Any
suggestions would be greatly appriciated
<?php
$today=date("d-m-Y");
// Make a MySQL Connection
mysql_connect("localhost", "chuck", "0Tnrivl33@^N") or die(mysql_error());
mysql_select_db("chuck_main") or die(mysql_error());
$query=mysql_query("SELECT frndescshort, envyear, frncolor, frnimageclick, FROM inventory WHERE frnsubtype = 'drlf' OR frnsubtype = 'stds'") or die(mysql_error());
$result=mysql_query($query);
$cols=3; // Here we define the number of columns
$echo "<table>"; // The container table with $cols columns
do{
echo "<tr>";
for($i=1;$i<=$cols;$i++){ // All the rows will have $cols columns even
// the records are less than $cols
$row=mysql_fetch_array($result);
if($row){
$img = $row['frnimageclick'];
echo "<td>";
echo "<table>";
echo '<tr valign="top">';
echo '<td><img src="http://www.sitepoint.com/forums/images/AntiquePics/Tables/$img"/></td>'; // columns can have both text and images
echo "<td>";
echo "<b><$row['frndescshort']></b><br/>";
echo "<$row['envyearl']><br/>";
echo "<$row['frncolor']><br/>";
echo "</td>";
echo '<td width="50"> </td>'; <!-- Create gap between columns -->
echo "</tr>";
echo "</table>";
echo "</td>";
}
else{
echo "<td> </td>"; //If there are no more records at the end, add a blank column
}
}
} while($row);
echo "</table>";
?>
Welcome to Sitepoint, joepikepark.
Have a play around with this, you should be able to modify it to suit. If you have any questions, just holla.
<?php
error_reporting(-1);
ini_set('display_errors', true);
function exitWithMessage($message){
echo $message;
exit;
}
$con = mysql_connect('localhost', 'chuck', '0Tnrivl33@^N');
if(false === is_resource($con)){
exitWithMessage('Cannot connect to database: ' . mysql_error());
}
if(false === mysql_select_db('chuck_main')){
exitWithMessage('Cannot select database: ' . mysql_error());
}
$sql = "SELECT frndescshort, envyear, frncolor, frnimageclick, FROM inventory WHERE frnsubtype = 'drlf' OR frnsubtype = 'stds';";
$res = mysql_query($sql);
if(false === is_resource($res)){
exitWithMessage('Cannot execute query: ' . mysql_error());
}
?>
<html>
<head>
<title>Demo</title>
</head>
<body>
<h4>We found <?php echo mysql_num_rows($res); ?> records.
<table>
<?php while($record = mysql_fetch_assoc($res)): ?>
<tr>
<?php foreach($record as $value): ?>
<td>
<?php echo $value; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
thanks alot. I’ll try it out
I tried to run the php file but go an error pointing to line 30. Am I missing
something? Also, I assume once I get this working that value will display
the entire record. How to I adjust it to display specific fields ?
thanks again for all of your help !
<?php
error_reporting(-1);
ini_set(‘display_errors’, true);
function exitWithMessage($message){
echo $message; exit;
}
$con = mysql_connect(‘localhost’, ‘chuck’, ‘0Tnrivl33@^N’);
if(false === is_resource($con)){
exitWithMessage('Cannot connect to database: ’ . mysql_error());
}
if(false === mysql_select_db(‘chuck_main’)){
exitWithMessage('Cannot select database: ’ . mysql_error());
}
$sql = “SELECT frndescshort, envyear, frncolor, frnimageclick, FROM inventory WHERE frnsubtype = ‘drlf’ OR frnsubtype = ‘stds’;”;
$res = mysql_query($sql);
if(false === is_resource($res)){
exitWithMessage('Cannot execute query: ’ . mysql_error());
}
?>
<html>
<head>
<title>Demo</title>
</head>
<body>
<h4>We found <?php echo mysql_num_rows($res); ?> records.
<table>
<?php while($record = mysql_fetch_assoc($res)): ?>
<tr>
<?php foreach($record as $value): ?>
<td>
<?php echo $value; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
Error :
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/chuck/public_html/displaytables.php on line 30
Try:
<?php
error_reporting(-1);
ini_set('display_errors', true);
function exitWithMessage($message){
echo $message;
exit;
}
$con = mysql_connect('localhost', 'chuck', '0Tnrivl33@^N');
if(false === is_resource($con)){
exitWithMessage('Cannot connect to database: ' . mysql_error());
}
if(false === mysql_select_db('chuck_main')){
exitWithMessage('Cannot select database: ' . mysql_error());
}
$sql = "SELECT frndescshort, envyear, frncolor, frnimageclick FROM inventory WHERE frnsubtype IN ('drlf', 'stds');";
$res = mysql_query($sql);
if(false === is_resource($res)){
exitWithMessage('Cannot execute query: ' . mysql_error());
}
?>
<html>
<head>
<title>Demo</title>
</head>
<body>
<h4>We found <?php echo mysql_num_rows($res); ?> records.
<table>
<?php while($record = mysql_fetch_assoc($res)): ?>
<tr>
<?php foreach($record as $value): ?>
<td>
<?php echo $value; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
Weird error though.