tags:

views:

72

answers:

4

Hi there,

I want to make my own function which performs in a similar manner to the following actual code.. ie

if(mysql_num_rows($res) == FALSE) {

  // DO SOMETHING BECAUSE THERE ARE NO RESULTS

}

In my code, i'm repeating an SQL statement a few times around the place, and if there are results, then I go ahead and do stuff.

What I'd like to do is create my own FALSE return in my own function ie

if(my_special_function($variable) == FALSE) {

    // DO STUFF

}

Is this as simple as in my special function having something like...

function my_special_function($variable) {

   $sql = 'SELECT field FROM table WHERE something=$variable';
   $res = mysql_query($sql);

   if(mysql_num_rows($res) == FALSE) {

     return FALSE;

   } else {

     return TRUE;

   }
}

?

+1  A: 

I can see a few problems.

Variable interpolation does not happen in single quotes, also its advisable to not to substitute variable directly into queries.

   $sql = 'SELECT field FROM table WHERE something=$variable';

should be

   $sql = 'SELECT field FROM table WHERE something='.mysql_real_escape_string($variable);

mysql_num_rows returns false when there is a problem, say when its parameter is not a valid resource. If you really want to check the case of "no rows returned" you need to check its value for 0

codaddict
+1  A: 

Yes, that is all for a function. Why don't you try such things first before asking whether it works or not?

You should add additional error checking for mysql_query and replace $sql = 'SELECT field FROM table WHERE something=$variable'; by:

$sql = 'SELECT field FROM table WHERE something='.$variable;
Lekensteyn
I guess I could have tried I just didn't know if this was a hack or considered proper coding. I have invented my own way of doing things in the past without checking in with good programmers like yourself only to find it was a security risk or it only worked because of a bug in the PHP or something like that. Hence I run through new things here as what is obvious to you guys, isn't that obvious to me. Cheers!!
KeenLearner
This are basic language constructs. You should get familiar with them. Just to note, `if(somefunc() == FALSE)` is the same as `if(!somefunc())`. The PHP Manual has a [nice table about comparing types](http://nl3.php.net/manual/en/types.comparisons.php).
Lekensteyn
+1  A: 

I'd revise it a bit

function my_special_function($variable) {
   $sql = "SELECT field FROM table WHERE something=$variable";
   $res = mysql_query($sql);

   if(mysql_num_rows($res)) {
     return TRUE;
   }
   return FALSE;
}

Changes

  1. No need for the else { ... } as if the if evaluation is true, it won't go any further, if it isn't, then FALSE is returned.
  2. Changed the if statement to if(mysql_num_rows($res)) as mysql_num_rows() will return FALSE on failure, and a number on everything else. So, if there's 0 affected rows, or an error you won't get the return TRUE.
  3. Inside your $sql variable you had single quotes, the literal $variable would be passed rather than what was passed to the function
Robert
if you use `return mysql_num_rows($res) > 0;` you don't need any if statement ;-)
captaintokyo
Yeah, but even though anything besides 0/false equates to true, the OP may want the actual return value of TRUE
Robert
I think this does return the value of TRUE. It does not return the number of rows. It returns either TRUE or FALSE.
captaintokyo
Ah, I reread it, I missed the "> 0" tidbit. My bad.
Robert
Robert guessed right. Returning TRUE is on the cards!! Although both methods rock.
KeenLearner
Mine returns TRUE too... that's what the discussion above is about.
captaintokyo
+3  A: 

You can make your special function even simpler:

function my_special_function($variable)
{
   $sql = "SELECT field FROM table WHERE something='{$variable}'";
   $res = mysql_query($sql);
   return mysql_num_rows($res) > 0;
}
captaintokyo
I think that's most simple, but I would use type-casting while returning: `return (boolean)mysql_num_rows($res)`
faileN
faileN: What would be the advantage?
VolkerK