Jump to content

PHP syntax?


bughunter2

Recommended Posts

I was building a function to simply retrieving results from an SQL query with error-checking.

Both functions should work the same I think, but they don't.

 

This function (also with a successful query) returns "SomeText" (for example, obviously):

    function sqlFetchResult($query)
    {
        $result = sqlQuery($query);
        $result = mysql_result($result, 0) or die(mysql_error());
        return $result;
    }

 

This function (with a successful query) returns 1:

    function sqlFetchResult($query)
    {
        $result = sqlQuery($query);
        return mysql_result($result, 0) or die(mysql_error());
    }

 

The second function looks pretty much the same to me as the first one, why do I need to insert an extra line to first get the result and put it into a variable and then return that variable?

Link to comment
https://forums.phpfreaks.com/topic/46915-php-syntax/
Share on other sites

They should both return the same result. I'm not sure what you mean by error checking because both functions have a distinct lake of error handling.

 

A better example would be.

 

<?php

function sqlFetchResult($query) {
  if ($result = mysql_query(mysql_real_escape_string($query))) {
    return mysql_result($result, 0);
  } else {
    return FALSE;
  }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/46915-php-syntax/#findComment-234402
Share on other sites

But they do not return the same, one returns the mysql_result as in the first example and the second just returns 1 or 0. I'm pretty sure because I've tested the script more than once.

 

I wanted to use mysql_error() in combination with die()/exit() because you then immediately see what's wrong and the script stops too. It should be noted that these functions are just being used temporarily for debugging.

Link to comment
https://forums.phpfreaks.com/topic/46915-php-syntax/#findComment-234642
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.