bughunter2 Posted April 13, 2007 Share Posted April 13, 2007 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 More sharing options...
Stooney Posted April 13, 2007 Share Posted April 13, 2007 well if im thinking right, the first one is returning the value of $result where as the second one is returning the the result of the mysql_result function (aka 0 or 1). i could be wrong Link to comment https://forums.phpfreaks.com/topic/46915-php-syntax/#findComment-228853 Share on other sites More sharing options...
bughunter2 Posted April 13, 2007 Author Share Posted April 13, 2007 The result of the function mysql_result() goes into the variable $result in the first example. So that should equal the value that would be used as return value in example two? Link to comment https://forums.phpfreaks.com/topic/46915-php-syntax/#findComment-228914 Share on other sites More sharing options...
bughunter2 Posted April 20, 2007 Author Share Posted April 20, 2007 bump, anyone? Link to comment https://forums.phpfreaks.com/topic/46915-php-syntax/#findComment-234367 Share on other sites More sharing options...
trq Posted April 21, 2007 Share Posted April 21, 2007 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 More sharing options...
bughunter2 Posted April 21, 2007 Author Share Posted April 21, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.