Jump to content

check if id exists...


mkosmosports

Recommended Posts

I want php to run and make a quick check to see if an id exists in mysql. This id features as a url parameter.
Currently I do by running a query and selecting all of the id's, putting them into an array and running a condition that if the id in the url isnt in that array show an error page.

Is there a quicker way of doing this, perhaps without a mysql_query, but using some other mysql function?

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/33678-check-if-id-exists/
Share on other sites

You still need a query but grabbing all rows is rediculous.

If the id is contained within the url, use it in your query.

[code]
<?php
  $sql = "SELECT id FROM tbl WHERE id = '{$_GET['id']}';";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      // id exists.
    }
  }
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/33678-check-if-id-exists/#findComment-157837
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.