Jump to content

Recommended Posts

<?php

function row_exists($id)
{
   $res = mysql_query("SELECT * FROM my_table WHERE my_id_field = {$id}");
   if(mysql_num_rows($res) === 1)
   {
      return TRUE;
   }
   else
   {
      return FALSE;
   }
}

 

example;

<?php
if(row_exists($id))
{
    // it exists
}
else
{
    // it doesn't exist
}
?>

Link to comment
https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458364
Share on other sites

<?php

function row_exists($id)
{
   $res = mysql_query("SELECT * FROM my_table WHERE my_id_field = {$id}");
   if(mysql_num_rows($res) === 1)
   {
      return TRUE;
   }
   else
   {
      return FALSE;
   }
}

 

example;

<?php
if(row_exists($id))
{
    // it exists
}
else
{
    // it doesn't exist
}
?>

 

Thats a bit overkill, don't you think?

Link to comment
https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458365
Share on other sites

Thats a bit overkill, don't you think?

 

How many times is he going to use it?

 

If more than once; create a function.

 

Could even extend that function to allow variable table and field names.. oh, the magic of such functions goes on and on.

 

Code repetition is bad.

Link to comment
https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458372
Share on other sites

Why are you selecting *... that is overkill... you only need to select one thing, e.g. the id...

 

 

Likely he's using MySQL and the MyISAM engine. "SELECT *" has the same performance as "SELECT field".

 

If it's innodb, that's different. Doubt it's innodb.

Link to comment
https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458378
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.