-2

I am having an issue with fetch assoc, it is returning an error saying it expects an object not a boolean but I checked and "results" is an object not a boolean, what could be causing this?

try{
$someSQL = "Call SomeSproc()";
$results = mysqli_query($connection,$someSQL);
}catch(Exception $ex)
{
echo("Error: " .  __LINE__ . " " .$ex);
}

print_r($results);//says I have 14 results
echo gettype($results);//prints object

while($result = mysqli_fetch_assoc($results)) 
{}

This is the error

mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 14 [type] => 0 ) object
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in "path"
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • A lot of php functions return false on erros. Have you tried an if(mysqli_fetch_assoc($results)===false) to see what's happening? – Marcelo Staudt Dec 13 '17 at 18:24
  • Maybe its duplicated. So, look at this post https://stackoverflow.com/questions/11347971/mysqli-fetch-assoc-expects-parameter-1-to-be-mysqli-result-boolean-given – dodoconr Dec 13 '17 at 18:24
  • @MarceloStaudt I will try that, and no dodoconr as you can see by my code my result is not returning a boolean or false from print_r – Nikkolas Olliff Dec 13 '17 at 18:32

1 Answers1

-1

Possibly you're overriding $results value with some boolean variable during while loop, so it could be a typo in your code which you haven't included.


Please double check your code, or try rewrite it by using the following example using procedural style:

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = mysqli_query($link, $query)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

    /* free result set */
    mysqli_free_result($result);
}

/* close connection */
mysqli_close($link);
?>

Here is the version using object-oriented style:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

    /* free result set */
    $result->free();
}

/* close connection */
$mysqli->close();
?>
kenorb
  • 155,785
  • 88
  • 678
  • 743