ohno Posted December 31, 2019 Share Posted December 31, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309769-uncaught-error-call-to-a-member-function-fetch_assoc/ Share on other sites More sharing options...
Barand Posted December 31, 2019 Share Posted December 31, 2019 Always put this line of code mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); before the line in your code that calls mysqli_connect() Quote Link to comment https://forums.phpfreaks.com/topic/309769-uncaught-error-call-to-a-member-function-fetch_assoc/#findComment-1573008 Share on other sites More sharing options...
ohno Posted December 31, 2019 Author Share Posted December 31, 2019 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 { } } Quote Link to comment https://forums.phpfreaks.com/topic/309769-uncaught-error-call-to-a-member-function-fetch_assoc/#findComment-1573009 Share on other sites More sharing options...
Barand Posted December 31, 2019 Share Posted December 31, 2019 Just before the" new mysqli()" line in that case. That's the statement that is connecting to your db server. Quote Link to comment https://forums.phpfreaks.com/topic/309769-uncaught-error-call-to-a-member-function-fetch_assoc/#findComment-1573010 Share on other sites More sharing options...
ohno Posted December 31, 2019 Author Share Posted December 31, 2019 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? Quote Link to comment https://forums.phpfreaks.com/topic/309769-uncaught-error-call-to-a-member-function-fetch_assoc/#findComment-1573011 Share on other sites More sharing options...
ohno Posted December 31, 2019 Author Share Posted December 31, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309769-uncaught-error-call-to-a-member-function-fetch_assoc/#findComment-1573012 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.