mysql query return empty result?

Hi, I am using php with mysql database.
I want to use mysql_query to check whether a record exist and insert a new record if it doesn't exist.

--------------------------------------------------------------------
<?php

/* Connect and select database */

$result = mysql_query("SELECT * FROM $mysql_table WHERE id='$record_id'") or die('Query failed: ' . mysql_error());

if (!$result){

echo 'Record not exist, inserting new row.<br />';
mysql_query("INSERT INTO $mysql_table VALUES ('$page_url', '1')") or die('Could not insert new row: ' . mysql_error());

} else {

echo 'Record exist';
/* do something else with the $result */

}
?>
----------------------------------------------------------------------

I ran this several times and it always echo 'Record exist'. However, when i check the database via some web based GUI, such a record doesn't exist. Any idea?

Thanks,
Canis

Comments

  • : Hi, I am using php with mysql database.
    : I want to use mysql_query to check whether a record exist and insert a new record if it doesn't exist.
    :
    : ---------------------------------------------------------------
    : <?php
    :
    : /* Connect and select database */
    :
    : $result = mysql_query("SELECT * FROM $mysql_table WHERE id='$record_id'") or die('Query failed: ' . mysql_error());
    :
    : if (!$result){
    :
    : echo 'Record not exist, inserting new row.<br />';
    : mysql_query("INSERT INTO $mysql_table VALUES ('$page_url', '1')") or die('Could not insert new row: ' . mysql_error());
    :
    : } else {
    :
    : echo 'Record exist';
    : /* do something else with the $result */
    :
    : }
    : ?>
    : ---------------------------------------------------------------
    : I ran this several times and it always echo 'Record exist'. However, when i check the database via some web based GUI, such a record doesn't exist. Any idea?
    :
    : Thanks,
    : Canis
    :


    Thanks for attention. I sorted that out today. I looked at all mysql functions in www.php.net and found that i should use:
    if(mysql_num_row($result)) rather than
    if(!$result)

    it seems that (!$result) doesn't return TRUE even if $result is actually empty.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion