Xeoncross Posted June 16, 2007 Share Posted June 16, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/55847-can-i-pass-resource-ids-around-like-this-mysql/ Share on other sites More sharing options...
Xeoncross Posted June 18, 2007 Author Share Posted June 18, 2007 Bump Quote Link to comment https://forums.phpfreaks.com/topic/55847-can-i-pass-resource-ids-around-like-this-mysql/#findComment-277125 Share on other sites More sharing options...
akitchin Posted June 18, 2007 Share Posted June 18, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55847-can-i-pass-resource-ids-around-like-this-mysql/#findComment-277127 Share on other sites More sharing options...
Xeoncross Posted June 19, 2007 Author Share Posted June 19, 2007 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. I didnt' know how PHP5 might take it or if there was a big no-no here... So thanks for you're reply! Quote Link to comment https://forums.phpfreaks.com/topic/55847-can-i-pass-resource-ids-around-like-this-mysql/#findComment-277955 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.