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
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

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.