Trying to output data as an array [duplicate] - php

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 3 years ago.
I made a table that holds post and likes and I'm trying to output the of everything in the table but I get the
error: mysqli_num_rows() expects parameter 1 to be mysqli_result,
boolean given on line 18.
$sql3 = "SELECT * FROM post;";
$result = mysqli_query($conn,$sql3);
$datas = array();
if (mysqli_num_rows($result)> 0){
while($row = mysqli_fetch_assoc($result)){
$datas[] = $row;
}
}
foreach ($datas as $data){
echo $data;
}
I expect the all the information to be outputted, but the actual output is
mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean
given on line 18.

This means that your query is returning false instead of a mysqli_result object. The problem appears to be from the semicolon at the end of your statement. I believe that mysqli will see that as a multistatement. From https://www.php.net/manual/en/mysqli.quickstart.multiple-statement.php:
Multiple statements or multi queries must be executed with mysqli_multi_query(). The individual statements of the statement string are separated by semicolon. Then, all result sets returned by the executed statements must be fetched.
mysqli has a method to get the last error it encountered:
https://www.php.net/manual/en/mysqli.error.php
Example:
if(!$result){
printf("Error message: %s\n", mysqli_error($conn));
}

Related

PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in [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 3 years ago.
I'm getting warning:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
Here's my code:
<?php
session_start();
$db=mysqli_connect("localhost","aaron","","demo");
$id=$_GET["id"];
$sql=mysqli_query($db,"SELECT * FROM usres");
$check=mysqli_fetch_array($db,$sql);
if(isset($_POST['update'])){
$id=$_POST['id'];
$name=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];
$bankbookno=$_POST['bankbookno'];
$adharno=$_POST['adharno'];
$pancard=$_POST['pancard'];
$result = mysqli_query($db, "UPDATE users SET name='$name',email='$email',password='$password',bankbookno='$bankbookno' ,adharno='$adharno',pancard='$pancard'WHERE id=$id");
header("location:view.php");
}
?>
mysql_fetch_array requires a mysqli_result as first parameter. You can obtain a mysqli_result as the return of the mysqli_query.
For example:
$db = mysqli_connect("localhost","aaron","","demo");
$sql = mysqli_query($db,"SELECT * FROM usres");
$check = mysqli_fetch_array($sql);
Also note that using $_POST['id'] directly (via $id in your case) in a SQL statement will enable SQL injection attacks against your application. There are plenty of different approaches, one of them being prepared statements.
mysqli_fetch_array needs only one param which returned by mysqli_query(), mysqli_store_result() or mysqli_use_result()
$sql=mysqli_query($db,"SELECT * FROM usres");
$check=mysqli_fetch_array($sql);//removed $db, which is not needed here
The mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both.
mysqli_fetch_array Parameter should be:
returned by mysqli_query(), mysqli_store_result() or mysqli_use_result()

PHP bind_param not binding parameter [duplicate]

This question already has answers here:
MySQLI Prepared Statement: num_rows & fetch_assoc
(5 answers)
Closed 5 years ago.
I am trying to search a table for specific items using a prepared statement in PHP. I am getting no errors, but also getting no record. Here is my code:
$items = [];
$search = "john";
if ($stmt = $this->con->prepare("SELECT * FROM phptest WHERE search = ?")) { //'john'";
$stmt->bind_param("s",$search);
$stmt->execute();
while ($row = mysqli_fetch_array($stmt)) {
$item = [];
$item['id'] = $row['id'];
$item['first'] = $row['search'];
$item['last'] = $row['data'];
array_push($items, $item);
}
}
return $items;
Now, when I don't use a prepared statement, and just SELECT * FROM phptest I get all the results in the table (including the item where search = 'john'). Furthermore, if I use the query SELECT * FROM phptest WHERE search = 'john' I get the one record where search = 'john'
But as soon as I turn it into the prepared statement, I get zero errors but zero records. I do get a warning:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
Which made me think my bind_param or execute() was returning FALSE, but when I check, it does not appear to be returning false.
I started off my adventure working through the tutorial https://www.simplifiedcoding.net/android-mysql-tutorial-to-perform-basic-crud-operation/, which I thought I understood fully but ran into my error when trying to make my own PHP API.
I then went to the manual http://php.net/manual/fr/mysqli.prepare.php, but still cannot find my error.
Though it has been closed as "off-topic," I have reviewed PHP bind_param not working and found nothing applicable to my situation.
Likewise, I am not finding the error in PHP bind_param not defined nor php bind_param is not working.
You're very close. mysqli_fetch_array() expects to be passed a result object, not the statement object itself:
$stmt = $conn->prepare(...);
$stmt->bind_param(...);
$stmt->execute();
$result = $stmt->get_result();
while ($row = mysqli_fetch_array($result)) {
Or, in the fully OO manner:
while ($row = $result->fetch_array()) {

PHP warning expects parameter 1 to be resource, object given [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 1 year ago.
I want to retrieve all data from a table, so I use this code
<?php
include("config.php");
$sql = "SELECT * FROM ".$USERS;
$sql_result = mysqli_query($connection, $sql);
if ($sql_result) {
while ($result = mysql_fetch_assoc($sql_result)) {
echo $result;
}
}
else {
die ('Could not execute SQL query '.$sql);
}
?>
but got this warning:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource,
object given in C:\xampp\htdocs\newSDP\phpscript\users.php on line 6
How can I fix it?
Change:
mysql_fetch_assoc to mysqli_fetch_assoc

Why is my PHP Mysqli code expecting a mysqli_result parameter [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 6 years ago.
I have a line of code in my php that reads:
$sel_venue = "SELECT 'char_type' FROM 'character_type_allowed' WHERE status='Open'";
$run_venue = mysqli_query($con,$sel_venue);
while ($row = mysqli_fetch_array($run_venue))
if ($row['char_type'] == 'Mortal')
{ print ("<li><a href='http://houston-by-night.com/sheets/create/mortal.php'>Create Mortal</a></li>"); }
The link associated with this does nothing. Zero interaction beyond acting likeit wants to expand. My error log produces this: Why is it asking for this?
[08-Aug-2016 23:28:41 America/New_York] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/houchat/public_html/incl/creation.php on line 8
You can't use ' as ticks for field/tablenames.
Your query is producing an error. You can see the error with mysqli_error($con).
Please see the corrected code below
$sel_venue = "SELECT `char_type` FROM `character_type_allowed` WHERE status='Open'";
$run_venue = mysqli_query($con,$sel_venue) or die(mysqli_error($con));
while ($row = mysqli_fetch_array($run_venue)) {
if ($row['char_type'] === 'Mortal') {
print ("<li><a href='http://houston-by-night.com/sheets/create/mortal.php'>Create Mortal</a></li>");
}
}
Your query failed, so $run_venue is the boolean false instead of what you expect. You should check for errors before you use any query result. Do this:
$run_venue = mysqli_query(...);
if(!$run_venue) die(mysqli_error($con));
... //<- we get here if the query succeeded
You will see the error. The problem is that your SQL statement wraps the table name between single quotes 'character_type_allowed', instead of backticks (backtick is above the tab key on my keyboard)

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.

Categories