简体   繁体   中英

How to check if a row exists in MySQL using PHP

I am trying to read in an XML file and compare it to fields in an existing database.

If the ID in the database doesn't exist in the XML file, then the whole row corresponding to the Id is no longer valid and will be deleted.

To do this I read in each line of the XML from start to finish in a while statement.

As step one I am trying to do a simple compare, and echo if it finds an Id in the database that doesn't exist in the XML.

I know there are some Ids in the database that don't exist in the XML, but the following code is not displaying them.

I've got three questions, firstly how would I display the Id that is pulled from the database, and secondly why isn't this code finding any ids that are not in the XML?

The final question is am I going about this completely the wrong way and is there a better way to do it!

$sql_result = mysql_query("SELECT id FROM `list` WHERE id =  $id") or die(mysql_error());

if($sql_result)
{
// echo $id . " Id exists " .  $sql_result["id"] . "\n";
}
else
{
echo "Id no longer exists" . $id . "\n";
}

Your code isn't finding what you expect because even though the id may not be found, $sql_result still holds a TRUE value because the query was successful. Instead, check if myqsl_num_rows() > 0

if($mysql_num_rows($sql_result) > 0)
{
  // echo $id . " Id exists "\n";

  //Now, to print the id, you need to fetch it from `$sql_result`, 
  //which is just a resource at this point:
  $row = mysql_fetch_assoc($sql_result);
  echo $row['id'];
}

This is the proper way to check:

$sql_result = mysql_query("SELECT `id` FROM `list` WHERE `id` =  ".intval($id,10)." LIMIT 0,1");

if(is_resource($sql_result) && mysql_num_rows($sql_result) > 0 ){
    $sql_result = mysql_fetch_assoc($sql_result);
    echo $id . " Id exists " .  $sql_result["id"] . "\n";
}
else{
    echo "Id no longer exists" . $id . "\n";
}

You should check the number of rows returned using mysql_num_rows(). Otherwise, you are simply checking to see if the query executed without any error.

if($sql_result)

to

if(mysql_num_rows($sql_result))

You can use NOT IN() on your select with the IDs that exist on you XML like:

SELECT id FROM `list` WHERE id NOT IN($your_id_list)

With this you'll have a list of IDs that are not in the list.

Your IDs must be separated with a comma like:

SELECT id FROM `list` WHERE id NOT IN(123,654,987,45)

Question 1: how would I display the Id that is pulled from the database?

$sql_result = mysql_query("SELECT `id` FROM `list` WHERE `id` =  $id") or die(mysql_error());
$sql_row = mysql_fetch_assoc($sql_result);
if(!empty($sql_row['id'])) {
    echo "Id exists - " .  $sql_row['id'] . "\n";
} else {
    echo "Id no longer exists - " . $sql_row['id'] . "\n";
}

Question 2: why isn't this code finding any ids that are not in the XML?

I think in your code the if() condition will always return true irrespective if the Id exists in the database or not. And secondly as you might have guessed from my code above, you are missing to fetch the data from the SQL resultset

Question 3: am I going about this completely the wrong way and is there a better way to do it?

You are doing it the right way by browsing through the XML and checking each entry in the database for existence. A better way might be to first retrieve all IDs from the XML and then use them in the single SQL query:

SELECT `id` FROM `list` WHERE `id` NOT IN ($list);

Please note that this query might run slow if there are a very large number of IDs in the XML file, say a few hundreds.

mysql_num_rows()

Or

SELECT COUNT(*) [...]

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