I'm kinda of stuck here for a while maybe you guys can help me out? I want to highlight a table of database record, coming from one table, and check if the id out of this table also exists in the 2nd table.
Let me be more specific:
Table 1: Projects (projectid pk)
Table 2: Reservations (projectid fk)
A simple sample code, to keep this simple:
while( $record=mysql_fetch_array($result) )
{
$projectid=$record['projectid'];
echo "<tr>". $projectid ."</tr>;
}
So this is working great, I get all table rows with each projectid which exists in my DB, but how to pass this $projectid
variables to another query which checks if this exists in the other table, and if it exists, echo out something different? Here's my code which probably doesn't make much sense, but at least the logic is there... I hope?:
while( $record=mysql_fetch_array($result) )
{
$projectid=$record['projectid'];
$hasreservationquery = ("
SELECT *
FROM reservelist rl
INNER JOIN project p on rl.projectid = p.projectid
WHERE rl.projectid = $projectid
");
$hasreservationqueryresult = mysql_query($hasreservationquery) or die(mysql_error());
if ($hasreservationqueryresult > 1)
{
echo "<tr id='hasreservation'>";
}
else
{
echo "<tr>";
}
echo "". $projectid ."";
}
Unfortunately this returns all tables highlighted, not just the one that really has a reservation in it. What am I doing wrong??
Thanks in advance.
You don't ever appear to be checking the row count. you need to change your if statement:
if (mysql_num_rows($hasreservationqueryresult) > 0)
This will now check if 1 or more results are returned (change it to be > x if you want x + 1 results returned before you highlight).
mysql_query doesn't return the amount of affected rows.
I think something like
if($hasreservationqueryresult && mysql_num_rows($hasreservationqueryresult) > 0)
Except for that I'd definitely arrange your code a bit better and rethink your variable names :)