Displaying mysql table data in html table.

I’m extending a web app that we use to manage tooling check-ins and check-outs. I’ve written some PHP that is designed to pull selected contents from a mysql table named “checkedout”, and display the contents onto a generated html page in table form. I know my SQL query is correct, if I run it straight against the db it returns the correct results, however, all I’m getting is an empty table when executing the page. Not sure where I’m going wrong here. I should also mention that I have very similar code running on other pages and it pulls the data and formats it nicely. Any help would be greatly appreciated!

Thanks

[php]<?php

//connect to server

define(‘DB_NAME’, ‘equipment’);
define(‘DB_USER’, ‘root’);
define(‘DB_PASSWORD’, ‘’);
define(‘DB_HOST’, ‘localhost’);

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
die('Could not connect: ’ . mysql_error());
}

//connect to database

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
die('Can’t use ’ . DB_NAME . ': ’ . mysql_error());
}

//query the database

$get_checkout_history = mysql_query(“select a.StudentID,b.FirstName,a.DateOut,a.DateIn,a.Accessories,a.Notes from checkedout a join students b on a.StudentID = b.StudentID order by a.DateOut DESC”);

//fetch the results / convert results into an array / print out to html table

$limit = 6;
$count = 0;

echo “

”;
echo “”;
while($rows = mysql_fetch_array($get_checkout_history)) :
	$id = $rows['a.StudentID'];
	$name = $rows['b.FirstName'];
	$dateout = $rows['a.DateOut'];
	$datein = $rows['a.DateIn'];
	$repair = $rows['a.Accessories'];
	$notes = $rows['a.Notes'];

  if($count < $limit) {
		
		if(count == 0) {
		echo "<tr>";
		}
		
		echo "<td>$id</td><td>$name</td><td>$dateout</td><td>$datein</td><td>$repair</td><td>$notes</td>";
		}else{
		
		$count = 0;
		echo "</tr><tr><td>$id</td><td>$name</td><td>$dateout</td><td>$datein</td><td>$repair</td><td>$notes</td>";
		}
		$count++;
endwhile;		

echo “

ID Name Checked Out Checked In Needs Repair? Notes
”;

mysql_close();
?>[/php]

Not sure what all the code==0 coding is all about, unless you are not showing all your code…
But, this is how I would create a table from this query and limit it to the first 6…
First, your SQL query is changed to do this for you… Next, a smoother display of the table…

[php]
$get_checkout_history = mysql_query(“select a.StudentID,b.FirstName,a.DateOut,a.DateIn,a.Accessories,a.Notes from checkedout a join students b on a.StudentID = b.StudentID order by a.DateOut LIMIT 6 DESC”);

echo “

”;
echo “”;

while($rows = mysql_fetch_array($get_checkout_history)) {
echo “

”;
echo “”;
echo “”;
echo “”;
echo “;
echo “”;
}
echo “
ID Name Checked Out Checked In Needs Repair? Notes
” . $rows[‘a.StudentID’] . “” . $rows[‘b.FirstName’] . “” . $rows[‘a.DateOut’] . “” . $rows[‘a.DateIn’] . “” . $rows[‘a.Accessories’] . "” . $rows[‘a.Notes’] . “
”;
[/php]

Something like that should work well for you… Simplest is always best-est!
Good luck, hope that helps you…

Sponsor our Newsletter | Privacy Policy | Terms of Service