Goafer Posted December 15, 2009 Share Posted December 15, 2009 Hey guys, please bear with me if any of this is obvious/stupid, it's been a while since i've done any coding. I agreed to write a small website for a friend and have been working on a very basic CMS to kick start the project using classes. When I use the following code nothing seems to happen (yes i am connected to the database with no problems) function queryDB($sql) { return mysql_query($sql) or die("Unable to perform query on database: ".mysql_error()); } accessed using $query = $this->queryDB($sql). if I try and parse this query forwards using: mysql_fetch_array($query); It returns an error saying the expected statement for mysql_fetch_array is boolean and cannot be processed. so I spent ages trying to figure out what on earth was going wrong to make it boolean. Turns out that if I remove: or die("Unable to perform query on database: ".mysql_error()) it starts working fine. Can anyone shed any light on why this is happening? Presumably if there is no error with the query the "OR die" shouldn't be doing anything and therefore should not be giving a boolean argument, but the problem seems to be stemming from the OR DIE section of the code. I have the same issue with the mysql_fetch_array argument, if I include an OR DIE, it does naff all, but if I leave it out it returns my array just fine... Any explanations much appreciated. *edit* Furthermore, when I use this: function queryDB($sql) { return mysql_query($sql) or die("Unable to perform query on database: ".mysql_error()); } it works okay, BUT if I use this: function fetchArray($query) { return mysql_fetch_array($query, MYSQL_ASSOC) or die("Unable to return data from database: ".mysql_error()); } it still won't do anything, I have to take out the or die completely for this statement to work. Like I said, it's been a while so maybe i'm missing something obvious but any knowledge shared would be great. Quote Link to comment https://forums.phpfreaks.com/topic/185215-error-in-queries/ Share on other sites More sharing options...
Goafer Posted December 15, 2009 Author Share Posted December 15, 2009 Sorry, just to add, the server is: Apache 2.2.11 PHP 5.3.0 MySQL 5.1.36 If that helps at all (any known bugs etc (I couldn't find any)) Quote Link to comment https://forums.phpfreaks.com/topic/185215-error-in-queries/#findComment-977742 Share on other sites More sharing options...
cags Posted December 15, 2009 Share Posted December 15, 2009 I don't know for certain, but I'd imagine it's not liking the combination of return and or die() being on the same line. Less lines of code is != better code, do you get an error using... $result = mysql_query($sql) or die("Unable to perform query on database: ".mysql_error()); return $result; On a side note you should probably look at using trigger_error rather than die. Quote Link to comment https://forums.phpfreaks.com/topic/185215-error-in-queries/#findComment-977756 Share on other sites More sharing options...
Goafer Posted December 15, 2009 Author Share Posted December 15, 2009 yup, tried that combination, returned the same errors, will look into trigger_error now thanks, Like I said (twice) it's been a while so a lot if the stuff I knew i've forgotten and even what I knew then wasn't great lol Quote Link to comment https://forums.phpfreaks.com/topic/185215-error-in-queries/#findComment-977789 Share on other sites More sharing options...
Goafer Posted December 15, 2009 Author Share Posted December 15, 2009 Okay so I get this error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\web\CMS\scripts\db.php on line 22 when I use: 16 function queryDB($sql) { 17 return mysql_query($sql) 18 or trigger_error("Unable to perform query on database: ".mysql_error()); 19 } If I leave out the OR trigger... so that the query actually executes, and then pass that into: 21 function fetchArray($query) { 22 return mysql_fetch_array($query, MYSQL_ASSOC) 23 or trigger_error("Unable to return data from database: ".mysql_error()); 24 } it breaks my browser for 30 secs (max runtime) then produces an unending loops of: Notice: Unable to return data from database: in C:\web\CMS\scripts\db.php on line 23 If I leave out the OR trig.... in BOTH functions, it seems to work. *confused* Presumably the un-ending loop of errors in the second case is from: while($row = $this->fetchArray($query)) { It's almost as if PHP is choosing not to do the primary task, but jump straight to the OR which is what i'm not understanding, because the query can and does execute perfectly fine in the absence of the OR Quote Link to comment https://forums.phpfreaks.com/topic/185215-error-in-queries/#findComment-977818 Share on other sites More sharing options...
PFMaBiSmAd Posted December 15, 2009 Share Posted December 15, 2009 Again - What trigger_error() outputs (by default an E_USER_NOTICE) is dependent on the error_reporting/display_errors/log_errors settings and on a majority of the systems people are using while trying to debug code, it won't output anything and its' use must also include instructions on setting error_reporting/display_errors/log_errors settings. Quote Link to comment https://forums.phpfreaks.com/topic/185215-error-in-queries/#findComment-977824 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.