Jump to content
Sign in to follow this  
bbmak

PHP Fatal error: Call to a member function prepare() on null

Recommended Posts

Hi

Trying to implement with the prepare statement, and it gives me this error.

"PHP Fatal error:  Call to a member function prepare() on null."

Anybody knows what is wrong? 

	function listMerchant() {
		
		$merchant = array();
		
		$merchantList = $this->mysqli->prepare("SELECT * FROM core_merchant");
		$merchantList->execute();
		while($merchantRows = $merchantList->fetch_array()) {
			
			$merchant[] = $merchantRows;
			
		}
		
		$merchantList->close();
		
		return $merchant;
		
	}

Share this post


Link to post
Share on other sites

It means there's no such thing as $this->mysqli. Either that attribute doesn't even exist, or you've failed to assign a connection to it.

Share this post


Link to post
Share on other sites

ummm.... I have other functions in the class using  $this->mysqli-query(), and they work fine. Somehow the prepare() is not working. I am new to prepare(), and not sure my syntax is correct or not. 

Edited by bbmak

Share this post


Link to post
Share on other sites

I go back and check my connection. The file include the wrong path for the connection folder. I fixed that, and now I got this.

PHP Fatal error: Call to undefined method mysqli_stmt::fetch_array()

Share this post


Link to post
Share on other sites

A prepared statement doesn't have a fetch_array() method. Read the manual on how prepared statements work.

 

Actually, you don't need this at all, because you have no external input. The entire method can be boiled down to

function listMerchant() {
    return $this->mysqli->query('SELECT col1, col2, ... FROM core_merchant')->fetch_all(MYSQLI_BOTH);
}
Edited by Jacques1

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

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