[英]PHP MYSQL - Highlight a database table row IF table1.id exists in table2
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. 我想突出显示一个数据库记录表,来自一个表,并检查该表中的id是否也存在于第二个表中。
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? 所以这很好用,我得到所有表格行,每个projectid存在于我的数据库中,但是如何将这个$projectid
变量传递给另一个查询,该查询检查是否存在于另一个表中,如果它存在,则回显不同的东西? 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语句:
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). 现在将检查是否返回了1个或多个结果(如果您希望在突出显示之前返回x + 1个结果,则将其更改为> x)。
mysql_query doesn't return the amount of affected rows. mysql_query不返回受影响的行数。
I think something like 我觉得有点像
if($hasreservationqueryresult && mysql_num_rows($hasreservationqueryresult) > 0) if($ hasreservationqueryresult && mysql_num_rows($ hasreservationqueryresult)> 0)
Except for that I'd definitely arrange your code a bit better and rethink your variable names :) 除此之外,我肯定会更好地安排你的代码并重新考虑你的变量名:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.