I am trying to upgrade an old application to PHP 7.2. It contains an sql class PHP file with the following function which I have modified to use mysqli:
function query($query, $index=0)
{
// query
if (!$this->res[$index] = mysqli_query($this->connection, $query))
{
// if query fails show error
$this->error('<strong>invalid query</strong>:<br />' . $query . '<br />');
return false;
}
// statistical information
$this->num_rows[$index] = @mysqli_num_rows($this->res[$index]);
$this->num_flds[$index] = @mysqli_num_fields($this->res[$index]);
$this->num_aff[$index] = @mysqli_affected_rows($this->connection);
$this->last_id = @mysqli_insert_id($this->connection);
return true;
}
This function throws the folling error:
E_WARNING Error in file �sql.class.php� at line 132: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given E_WARNING Error in file �sql.class.php� at line 133: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given
My initial thought was that the query was failing. However, including this line inside the function...
print_r(mysqli_fetch_assoc($this->res[$index]));
results in the following output:
Array ( [s_id] => 2088b4cc0d026c2742e8e0cb7d7c8e95 )
In the output above, the query is returning a session ID. That leaves me a bit confused because the value of $this->res[$index]
is not a bolean, yet the Warning says it is.
Edit:
If I include this in the function:
echo mysqli_num_rows($this->res[$index]);
echo mysqli_num_fields($this->res[$index]);
Each line echos the correct value of 1
but each line also produces the boolean Warning...
E_WARNING Error in file �sql.class.php� at line 125: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given
E_WARNING Error in file �sql.class.php� at line 126: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given
mysqli_query return values : If can be mysqli_result object
,true
, false
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
try this modification to see which query really successful but result was true
function query($query, $index=0)
{
$this->res[$index] = mysqli_query($this->connection, $query);
// query
if (is_bool($this->res[$index])) {
if ($this->res[$index] === false)
{
// if query fails show error
$this->error('<strong>invalid query</strong>:<br />' . $query . '<br />');
return false;
} else {
// query was successful, but the result is not a mysqli_result object
$this->warning('<strong>success with no returned data query</strong>:<br />' . $query . '<br />');
return true;
}
}
// statistical information
$this->num_rows[$index] = @mysqli_num_rows($this->res[$index]);
$this->num_flds[$index] = @mysqli_num_fields($this->res[$index]);
$this->num_aff[$index] = @mysqli_affected_rows($this->connection);
$this->last_id = @mysqli_insert_id($this->connection);
return true;
}
Check that $this->warning is exist in your code, or update it to what it will be proper