Jump to content

php/mysql help


uwannadonkey

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
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
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
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
Share on other sites

How can selecting EVERYTHING from a table be quicker than/the same as selecting only one thing? :o

 

It's just how the ISAM engine works. Do some performance benchmarks. :)

 

The only thing "SELECT *" does is give you a smidgen more data overhead.

Link to comment
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.