Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't put mysql_query call in external function? 1

Status
Not open for further replies.

OsakaWebbie

Programmer
Feb 11, 2003
628
JP
I thought I was being clever, but... Throughout many PHP files I had various versions of code to do the same thing: call mysql_query(), check for a FALSE result, and exit the script while displaying part or all of the useful information (i.e. sometimes I would show the complete SQL statement, sometimes I wouldn't; sometimes I would enclose it in <pre> but most of the time I wouldn't...). So I decided to consolidate and clean it up. I have a file called functions.php that is included in all my other files, so I put this in functions.php:
Code:
function sqlquery_checked($sql) {
  return mysql_query($sql) or die("<pre><strong>SQL Error ".mysql_errno().": ".mysql_error()."</strong><br />($sql)</pre>");
}
Then I tried calling it in one of my main files:
Code:
$result = sqlquery_checked("SELECT stuff FROM table");
while ($row = mysql_fetch_object($result)) {
But something was lost in transmission:
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in...
I'm assuming that the call to mysql_query succeeded, because my die call did not run. So I should have had a valid result to pass back. Are MySQL result resource pointers not passable as the return value of a function or something? Or is the query result information only visible inside my functions.php file?
 
the die won't trigger because the script can return something. in this case a boolean false

expand a bit on your abstracted function and it should work
Code:
function sqlquery_checked($sql) {
  $result = mysql_query($sql);
  if ($result === false ){
     die("<pre><strong>SQL Error ".mysql_errno().": ".mysql_error()."</strong><br />($sql)</pre>");
   }
   return $result;
}
 
When I first read your post, I thought you were saying that my SQL query must be actually failing but I didn't know it because of the way my logic statement was constructed. I immediately recognized that implication for a failed query, but couldn't see how that would affect a successful query. But indeed, it does - I don't fully understand why, but it's not important. Thanks!
 
You are correct. It will not affect instances where the query was successful (ie returned a resource handle, not Boolean false). In either case the s riot would have returned the result of the query and never 'die'd or given you an error message.

I had assumed you were deliberately passing duff SQL to test it.
 
Nope, I hadn't done any hostile testing yet - I was just sending it the queries that had always been in my code. In two different files, two different valid queries, my original code failed and yours worked. So you don't know why, either? Hmm, it will remain a mystery, I guess, because now that I have something that works, I don't see the need to try different versions just for experimentation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top