简体   繁体   中英

mysql_num_rows return 0 in the second query

I have this code:

$sql    = "SELECT 
               t.id AS txn_id, 
               g.id AS guest_id, 
               a.id AS booking_id 
           FROM 
               bookings a
           LEFT JOIN transactions t ON a.txn_id=t.id
           LEFT JOIN guests g ON g.txn_id=t.id
           WHERE 
               t.code='o2-i1321209942'
           LIMIT 1";

$query  = mysql_query($sql);

$exist  = mysql_num_rows($query);

if(!$exist) {
     echo 'NOT EXISSSTTTT!!!!!!!!!!!!!!!!';
} else {
     echo 'EXISSSTTTT!!!!!!!!!!!!!!!!';
}

Note: data exists in the database

When I click submit from a form it will display "NOT EXISSSTTTT!!!!!!!!!!!!!!!!" and then when I refresh the page or press Ctrl+F5 it will display "EXISSSTTTT!!!!!!!!!!!!!!!!" and when I refresh it again it will display "NOT EXISSSTTTT!!!!!!!!!!!!!!!!" again.

It acts like:

Submit: Not Exists

Refresh: Exists

2nd Refresh: Not Exists

3rd Refresh: Exists

Any suggestion why is it working weird? Any answer will be appreciated.

Thanks.

Your code outputs "not exist" if the query returns 0 rows or the query fails ( mysql_num_rows returns FALSE in case of error). So I guess you have a connection related problem (or maybe you construct query dynamically and not escape some data). You can track it down by outputting error information:

$query  = mysql_query($sql) or die(mysql_error()) ;

$exist  = mysql_num_rows($query);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM