简体   繁体   English

PHP MYSQL - 突出显示数据库表行IF table1.id存在于table2中

[英]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.

相关问题 PHP MYSQL JOIN QUERY 2 数据库,其中 table1.id = table2.id,如何将 table2.content 显示为 table1.id - PHP MYSQL JOIN QUERY 2 Databases, Where table1.id = table2.id, How to display table2.content as table1.id SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause Laravel 一页杂物 Select 全部来自 table2 其中 id = table1.id - Laravel one page crud Select all from table2 where id = table1.id MySQL Select语句where table1.id!= table2.id - MySQL Select statement Where table1.id != table2.id 突出显示表的选定行(该表由PHP从MySQL数据库生成) - Highlight selected row of the table (table is generated by PHP from MySQL database) 通过在Table1.id上应用groupBy,但在Table2上没有软删除的行,使用Laravel查询生成器来计算总和? - Calculate sum using Laravel Query Builder by applying groupBy on Table1.id but without soft deleted rows on Table2? 当我使用此php时,结果显示所有表而不是table1.id = table2.id的特定条件 - When i use this php the results show all the table not the specific condition that table1.id = table2.id MYSQL,PHP:仅当项目ID不在第二个表中时,才从表1插入INTO table2 - MYSQL, PHP: INSERT INTO table2 from table 1 only if the project ID is not in second table MySQL使用table1.id REGEXP table2.id为同一组选择不同的行 - MySQL select distinct rows for same group using table1.id REGEXP table2.id 该表或该表中存在mysql id - mysql id exists in this table or that table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM