mysqli_num_rows() expects parameter 1 to be mysqli_result, array given - php

I've been trying to look deep into the script, but I still can't find the problem.
here's the script
global $con;
$a= "SELECT id_list FROM list ORDER BY id_list";
$b = mysqli_query($con, $a);
$c= mysqli_fetch_assoc($b);
if(mysqli_num_rows($c)==0){
echo'0';
}else{
echo '1';
}
the error that appear is
mysqli_num_rows() expects parameter 1 to be mysqli_result, array given

You have to use mysqli_result onject as a parameter to mysqli_num_rows() so try:
mysqli_num_rows($b)
read manual here link

Related

Query returns results but Warning about boolean given [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 1 year ago.
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 boolean, 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

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in E:\xampp\htdocs\berita\menuBerita.php on line 14

I am new to PHP and i am getting these errors:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in E:\xampp\htdocs\berita\menuBerita.php on line 14
Here is my code:
<?php
include "koneksi.php";
$ambil_recent = mysqli_query("select id_berita,judul,tanggal from tbl_berita order by rand() limit 20",$koneksi);
?>
<?php
while($hasil_recent= mysqli_fetch_array($ambil_recent)){
echo "<li><a href='index.php?link=lihatDetailBerita.php&id=$hasil_recent[id_berita]'>".$hasil_recent['judul']."</a></li>";
}
?>
Please change the code
<?php
include "koneksi.php";// db connection $koneksi may you connection string or variable.
$ambil_recent = mysqli_query($koneksi,"select id_berita,judul,tanggal from tbl_berita order by rand() limit 20");
?>
Please note the synatx mysqli_query($connectionVar,$qry) You have made the mistake in passing the wrong parameter to the mysqli_query function.
Just change it and run your code . This will work
You could use something similar to the below:
<?php
$ambil_recent = mysqli_query($koneksi,"select id_berita,judul,tanggal from tbl_berita order by rand() limit 20");
while($hasil_recent= mysqli_fetch_array($ambil_recent)) {
//do stuff
}
?>

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
INSERT query produces "Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given"
(2 answers)
Closed 2 years ago.
Why am I getting this error message:
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given
My code is:
$statement = "INSERT INTO table1 (data1, data2) VALUES ('$variable1', '$variable2')";
if ($result = mysqli_query($conn,$statement)) {
echo "New record added successfully";
} else {
echo "Error adding records: " . $result . "<br>" . mysqli_error($conn);
}
echo "Adding records finished. ";
mysqli_free_result($result);
As stated in the mysqli_query manual:
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.
Your insert query will return true or false, but not an object. So, calling mysqli_free_result will not work here.

Php error when using mysqli

I've decided to using mysqli instead of mysql and I've having some errors, this is my first time using mysqli and I don't know what the errors are, any suggestions?
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /home/u250000297/public_html/forum/system/db.php on line 33
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/u250000297/public_html/forum/system/db.php on line 33
Line 32-36
function query($query) {
$sql = mysqli_query($query, $this->db) or die(mysqli_error());
return $sql;
mysqli_free_result($sql);
}
Line 44-48
function fetch($query) {
$sql = mysqli_fetch_array(mysql_query($query, $this->db));
return $sql;
mysqli_free_result($sql);
}
Try this, You have used mysql_query instead of mysqli_query
function query($query) {
$sql = mysqli_query($this->db, $query) or die(mysqli_error());
...
}
function fetch($query) {
mysqli_fetch_array(mysqli_query($this->db, $query));
....
}
instead of
function fetch($query) {
mysqli_fetch_array(mysql_query($query, $this->db));
...
}
Try using a mysqli_query() function like this maybe
$data = mysqli_query($dbc, $query);
mysqli_fetch_array($data)
Notice that with mysqli_query() two arguments are passed:
the database connection variable
the query variable
Then use the the result of the mysqli_query() function as the argument to mysqli_fetch_array() function :D

PHP error: Warning: ociparse() parameter 1 to be resource

As there is not ready made function in oracle to validate the Query so created.
So I tried below code to check whether the QUERY is valid or not.
if(isset($_POST['btn_Submit_Query']))
{
$check_query=$_POST['txtQuery'];
echo $check_query;
$valid = false;
$stmt = oci_parse($DB, $check_query);
echo "Statement" . $stmt;
//oci_define_by_name($stmt, 'NUMBER_OF_ROWS', $number_of_rows);
oci_execute($stmt, OCI_DEFAULT);
echo oci_num_rows($stmt);
}
I got following Warnings in the Execution:
Warning: oci_parse() expects parameter 1 to be resource, object given in D:\xampp\htdocs\app\DashBoardSite\Admin\querybuilder.php on line 899
Statement
Warning: oci_execute() expects parameter 1 to be resource, null given in D:\xampp\htdocs\app\DashBoardSite\Admin\querybuilder.php on line 902
Warning: oci_num_rows() expects parameter 1 to be resource, null given in D:\xampp\htdocs\app\DashBoardSite\Admin\querybuilder.php on line 903
Where is my mistake?
You must first connect to the database. This connection must a "resource" to variable $DB.

Categories