Jump to content

Uncaught Error: Call to a member function fetch_assoc


Recommended Posts

Hi everyone, Happy New Year in advance! I have the following error occurring :-

PHP Fatal error: Uncaught Error: Call to a member function fetch_assoc() on bool in /home/website/public_html/include/database.php:31

Line 31 :-

while ($row = $query_result->fetch_assoc()) { $result[] = $row; }

Which is in this query :-

public function DbGetAll($queryString) {
			$query_result = $this->db->query($queryString);
			$result = array();
			while ($row = $query_result->fetch_assoc()) { $result[] = $row; }
			return $result;
		}

 

I'm guessing it's a problem with the sql query but I'm not sure how to fix? The error log also references this file :-

#0 /home/website/public_html/data_objects/do_catalogue.php(68): DbManager->DbGetAll('SELECT p.code, ...')
#1 /home/website/public_html/smarty_plugins/function.load_search.php(38): DoCatalogue->SearchProducts(Array)
#2 /home/website/public_html/smarty_plugins/function.load_search.php(5): Search->init()

But I ***think*** that is just because the above query is incorrect?

Thank you for any help in fixing this error.

I don't see mysqli_connect() :-

 

class DbManager {
		
		public $db;
		
		function __construct() {
			$this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
			if ($this->db->connect_errno) {
				die("Failed to connect to MySQL: (" . $this->db->connect_errno . ") " . $this->db->connect_error);
			}
            if (!$this->db->set_charset("utf8")) {
                die("Error loading character set utf8: " . $this->db->error);
            } else {
                
            }
		}

 

OK, this is how it looks now:-

 

 

class DbManager {
		
		public $db;
		
		function __construct() {
			mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
			$this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
			if ($this->db->connect_errno) {
				die("Failed to connect to MySQL: (" . $this->db->connect_errno . ") " . $this->db->connect_error);
			}
            if (!$this->db->set_charset("utf8")) {
                die("Error loading character set utf8: " . $this->db->error);
            } else {
                
            }
		}

The error log now gives this :-

Uncaught mysqli_sql_exception: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation 'like' in /home/website/public_html/include/database.php:30

Looking at the DB it seems some columns have collation set to utf8mb4_unicode_ci while others are latin1_swedish_ci! I guess I just need to change the collation of those so they are all utf8mb4_unicode_ci?

I backed up the DB and changed them so they are all utf8mb4_unicode_ci. I also changed this line :-

if (!$this->db->set_charset("utf8")) {

to this :-

if (!$this->db->set_charset("utf8mb4")) {

And it seems to have fixed the fault :) Thanks for your help.

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.