Jump to content

Cant figure out PHP/MySQL Error


Karaethon

Recommended Posts

I am getting Fatal error: Uncaught Error: Call to a member function real_query() on null  with this code:

public final function Retrieve($TABLE, $CRIT){
  $_query = "SELECT * FROM `{$TABLE}` WHERE ";
  foreach($CRIT as $_field => $info){
    $_query .= " `{$_field}` = `{$info}` &&";
  }
  if($this->LINK->real_query(rtrim($_query, ' &'))){
    return $this->LINK->store_result();
  } else{
    return json_encode(array("Error"=>$this->LINK->errno(), "Description"=>$this->LINK->error()));
  }
}

(LINK is my mysql_connect() result.) I have tried everything i can think of, ->query, going to mysqli_query, breaking it sown and using a $result variable, but nothing seems to work...

Edited by Karaethon
Link to comment
Share on other sites

Good catch! I want to copy and paste my __construct code to show what I had and I noticed it was $LINK = mysqli.... instead of $this->LINK = mysqli....

I looked at it 500 times but kept missing it because I saw what I expected, not what was.

Link to comment
Share on other sites

16 minutes ago, ginerjm said:

Now that it has been pointed out to me that 2 &'s are necessary I suddenly realize that I have never seen anyone NOT use 'AND' in their queries.  For PHP, yes && is the rule.

MySQL also supports || and !, but SQL has been designed to look human-readable so people normally use OR and NOT instead. As opposed to PHP's && and ||, which are actually subtly different than "and" and "or".

Link to comment
Share on other sites

2 hours ago, requinix said:

As opposed to PHP's && and ||, which are actually subtly different than "and" and "or".

Just to confirm Requinix's statement by psedocode examples

This works:

result = query(sql) or die(error message)

Whereas this doesn't

result = query(sql) || die(error message)

 

Link to comment
Share on other sites

18 hours ago, Barand said:

Just to confirm Requinix's statement by psedocode examples

This works:


result = query(sql) or die(error message)

Whereas this doesn't


result = query(sql) || die(error message)

 

Hmm, yeah I kinda knew that, never really thought of how it would affect execution... 

In this case I dont think I could use AND though because the rtrim after completion could remove more than the AND? or maybe not it is only trimming from end....

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.