Jump to content

Error in queries...


Goafer

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.