-1

I am getting Fatal error: Call to a member function fetch_assoc() on a non-object in directoryhere/database.php on line 13

Here is the code in database.php file

<?php
$db = @new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE); 

if($db->connect_error){
    die("<h3>Could not connect to the MySQL server.<br />Please be sure that the MySQL details are correct.</h3>");
}

function html($string) {
    return htmlentities(stripslashes($string), ENT_QUOTES, "UTF-8");
}

$config = $db->query("SELECT * FROM config");
$config = $config->fetch_assoc();
?>

Tell me how can I get rid of this? PHP Version - 5.5.38 I am trying to install this uptime checker script.

  • 1
    You should add some proper [error handling](http://php.net/manual/en/mysqli.error.php) when making your queries. You should also not suppress errors using `@` since that will _hide_ any error message, making your code way harder to debug. Handle errors instead of hiding them. – M. Eriksson Jan 30 '19 at 06:55
  • Probably should see if the results coming from the query is true or false. if ($config) then access the resultset, else show an error message? – NiteRain Jan 30 '19 at 06:55
  • If you are still using PHP 5 I strongly recommend to upgrade as soon as possible. This version is no longer supported. [Let Rasmus Lerdorf explain it to you](https://youtu.be/wCZ5TJCBWMg?t=2434) – Dharman Dec 21 '19 at 19:25

1 Answers1

0

You can try that way ...............

$config = "SELECT * FROM config";
$result=mysqli_query($con,$config );
$config = mysqli_fetch_assoc($result);
  • 4
    The OP is using the OOP version of mysqli. Why should they use the less verbose functions instead? What would that solve? – M. Eriksson Jan 30 '19 at 06:56
  • 4
    Some explanation of why OP should *You can try that way* would also be useful. (although not sure in this case) – Nigel Ren Jan 30 '19 at 06:57