Jump to content

Recommended Posts

Is this a safe way to handle mysql errors and to return the result? So far it works fine but I am a little concerned that after I run this function the  $result["result"] might not return to the script the right way. (simplified versions of my code)

 

<?php
...
...
function run_mysql_query($query) {

//Run the query and suppress any errors.
$result = @mysql_query($query);

//If there was a problem with the script then we need to print out an ERROR instead of the results...
if (!$result) {
   
	$output["success"] = FALSE;
	$output["error"] = '<h1>MySQL error</h1>';
   
} else {//Else everything is fine - so lets return the result
	$output["success"] = TRUE;
	$output["result"] = $result;
}
   
return $output;
}
...
...
?>

 

Here is how I use it:

 

<?php
...
...
$query = 'SELECT * FROM `comments` WHERE `active` = \'1\' ORDER BY `time` DESC LIMIT 10';
$result = run_mysql_query($query);

//If there was a problem with the script then we need to print out an ERROR instead of the results...
if ($result["success"] != TRUE) {
   
	//Print the error...
	print $result["error"];
   
} else {//Else everything is fine - so lets get those comments!
   
	if (mysql_num_rows($result["result"]) != 0) { //If there are more than 0 results (meaning there ARE some results...)
	   
		//While there are results from the query...
		while ($row = mysql_fetch_assoc($result["result"])) {              <------- [b]This is what scares me.....[/b]
...
...
?>

 

Like I said it works fine with errors or with results - but I don't know if there might be something I am missing...? Can I just pass MySQL resource ID's around through functions like this with no worries? Is there are better way to do this?

you can pass the resource ID around as much as you want within the same script, provided you don't close the connection or end the script (in which it'd be irrelevant anyway) - have you tried your code?  it seems like it'd work just fine.

 

this is how i used to do my pagination; my function would run the query and return the resource ID for the resultset sought, then i was free to process the results however i pleased.

 

all you're doing by passing the resource ID back from the function is bumping it upward in scope.  you're not changing the actual resource.

all you're doing by passing the resource ID back from the function is bumping it upward in scope.  you're not changing the actual resource.

That is what I thought  :)

 

I have tried the above code and it works fine on PHP4 with MySQL 4 - MySQL errors or no MySQL errors.

However, as we all know - just because it works doesn't mean it is right.  ;D

 

I didnt' know how PHP5 might take it or if there was a big no-no here...

So thanks for you're reply!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.