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 2 years ago.
<?php
$queryuser = "SELECT id, email, firstname, lastname, address, city, country FROM users WHERE role = 'client'";
$resultuser = $connection->query($queryuser);
if ($resultuser->num_rows > 0)
{
// output data of each row
while ($rowuser = $resultuser->fetch_assoc())
{
$id_user = $rowuser['id'];
$firstname = $rowuser['firstname'];
$lasttname = $rowuser['lastname'];
$email = $rowuser['email'];
$phone = $rowuser['phone'];
$city = $rowuser['city'];
$state = $rowuser['state'];
$address = $rowuser['address'];
?>
Perhaps you've copied code from this page https://www.w3schools.com/php/php_mysql_select.asp
Please understand code line by line first. You have not define $connection variable in your code (may be it is a intentional approach for security).
Your notice saying that you are trying to get num_rows property from the object $resultuser but in this query $resultuser is not a object due to any error/faults of your query because query() returns FALSE on failure and object on success.
To confirm, you can check your result set by using var_dump();
Please replace this line if $resultuser->num_rows > 0 with (!empty($resultuser->num_rows) && ($resultuser->num_rows > 0)))
Related
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 want to fetch data with the prepared statements but getting the following answer. I also tried to follow previous solved question related to this which was given on StackOverflow but failed. Help me to solve the issue.
Fatal error: Uncaught Error: Call to a member function execute() on bool
<?php
$stmt = $mysqli->prepare("SELECT id,addpage,post_name FROM detail WHERE id = ? ");
$stmt->execute();
$result = $stmt->get_result();
var_dump($row = $result->fetch_all(MYSQLI_ASSOC));
echo $row['id'];
echo $row['addpage'];
echo $row['post_name'];
?>
mysqli_prepare returns bool (false) on error, in this case likely because you forgot to add the parameters.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $mysqli->prepare("SELECT id,addpage,post_name FROM detail WHERE id = ? ");
$stmt->bind_param('id', $some_defined_id);
$stmt->execute();
$result = $stmt->get_result();
var_dump($row = $result->fetch_all(MYSQLI_ASSOC));
echo $row['id'];
echo $row['addpage'];
echo $row['post_name'];
Also, see the documentation at https://www.php.net/manual/en/mysqli.prepare.php
This question already has answers here:
How to fetch all in assoc array from a prepared statement?
(4 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
I am trying to fetch TaxRate data from a table and I'm trying to select the first array only. However, I keep getting this error
"Fatal error: Uncaught Error: Call to a member function fetch_array()
on boolean in C:\xampp\htdocs\flowerique\shoppingCart.php:77 Stack
trace: #0 {main} thrown in C:\xampp\htdocs\flowerique\shoppingCart.php
on line 77"
Does anyone know the cause of this error and how do I fix it? Thanks!
This is my code :
//Retrieve Current GST Pricing
$qry = "SELECT * FROM gst GROUP BY EffectiveDate DESC";
$stmt = $conn->prepare($qry);
$stmt->execute();
//$stmt->close();
$result = $conn->query($qry);
$row = $result->fetch_array(); // This is line 77
while($row["EffectiveDate"] < date("Y-m-d"))
{
$row = $result->fetch_array();
}
$currentTaxRate = $row["TaxRate"];
This question already has answers here:
How do I solve "Parameter must be an array or an object that implements Countable?" [duplicate]
(2 answers)
Closed 2 years ago.
I write this script in roder to verify if the sql empty or not:
if (isset($_GET['edit'])){
$id = $_GET['edit'];
$update = true;
$result = $mysqli->query("SELECT * FROM task WHERE id_task=$id") or die($mysqli->error());
$count = count($result);
if($count == 1){
$row = $result->fetch_array();
$nom_task = $row['name_task'];
$id_operation = $row['id_operation'];
$nom_operation = $row['name_operation'];
}
any suggetions to resove this please? the problem is in $count and $result?????
mysqli::query returns a mysqli_result object (see https://www.php.net/manual/en/mysqli.query.php)
That object is not countable (meaning it cannot be used as a parameter in the count function)
If you want to know the number of rows returned by your SQL request, use $num_rows property from that mysqli_result object :
$result = $mysqli->query('SELECT ...');
$count = $result->num_rows;
Also as a (very important) side note, your code is vulnerable to SQL Injection, please see : https://stackoverflow.com/a/601524
Hey try this code snippet:
if (isset($_GET['edit'])){
$id = $_GET['edit'];
$update = true;
$result = $mysqli->query("SELECT * FROM task WHERE id_task=$id") or die($mysqli->error());
if(!empty($result) && count($result) == 1){
$row = $result->fetch_array();
$nom_task = $row['name_task'];
$id_operation = $row['id_operation'];
$nom_operation = $row['name_operation'];
}
Simply check the array if it's empty or not before count check.
This question already has answers here:
Error Exception Trying to access array offset on value of type null [closed]
(2 answers)
Closed 2 years ago.
I'm building a social media web app, and while I can successfully display posts to a feed, I also get these error messages:
Notice: Trying to access array offset on value of type null in /opt/lampp/htdocs/social/includes/classes/User.php on line 34
Notice: Trying to access array offset on value of type null in /opt/lampp/htdocs/social/includes/classes/User.php on line 37
These error messages repeat a number of times, and are displayed on the feed:
Here's the code that the error messages point to:
public function getFirstAndLastName() {
$username = $this->user['username']; //ERROR line 34
$query = mysqli_query($this->con, "SELECT first_name, last_name FROM users WHERE username='$username'");
$row = mysqli_fetch_array($query);
return $row['first_name'] . " " . $row['last_name']; // ERROR line 37
}
And here's my constructor for a user:
public function __construct($con, $user){
$this->con = $con;
$user_details_query = mysqli_query($con, "SELECT * FROM users WHERE username='$user'");
$this->user = mysqli_fetch_array($user_details_query);
}
The database has values for the first and last names, and they're written in the same format. Why am I getting a null value even though I'm able to post to the app?
It's not in array (and actually notice is pretty clear about that). Your $this->user is null which means you need to rather check where you set it and why it did not work (or maybe you try to access it too early, before it's set?).
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 6 years ago.
I've been getting one warning and one fatal error when trying to run the execute() command. I can't seem to find the error at my stm->execute();
Here are my errors:
Warning: mysqli_stmt::bind_param(): Invalid type or no types specified in C:\xampp\htdocs\library\admin-reg.php on line 20
Fatal error: Call to a member function execute() on boolean in
C:\xampp\htdocs\library\admin-reg.php on line 33
Then this is a snippet of my code.
<?php
#CONNECTION
require 'dbcon.php';
#INPUT CONTAINERS
$ACTIVATION_CODE=$USERNAME=$PASSWORD=$EMAIL=$FIRST_NAME=$MIDDLE_NAME=$LAST_NAME=$CONTACT_NO=$ADDRESS=$PROFILE='';
#ERROR CONTAINERS
$USERNAME=$PASSWORD=$EMAIL=$FIRST_NAME=$MIDDLE_NAME=$LAST_NAME=$CONTACT_NO=$ADDRESS=$PROFILE=$USER_TYPE='';
#CONNECTION/QUERY#
$connection = mysqli_connect('localhost','root','','library_system') or die(mysqli_error());
$stm = $mysli_link->prepare("INSERT INTO users (username,password,first_name,middle_name,last_name,address,contact_no,email,activation_code) values(?,?,?,?,?,?,?,?,?)");
$stm = $stm->bind_param($USERNAME,$PASSWORD,$FIRST_NAME,$MIDDLE_NAME,$LAST_NAME,$ADDRESS,$CONTACT_NO,$EMAIL,$ACTIVATION_CODE);
#VERIFICATION SCRIPT
IF(isset($_POST['register'])){
$USERNAME = trim(mysqli_real_escape_string($connection,$_POST['username']));
$PASSWORD = trim(mysqli_real_escape_string($connection,password_hash($_POST['password'],PASSWORD_DEFAULT)));
$EMAIL = trim(mysqli_real_escape_string($connection,$_POST['email']));
$FIRST_NAME = mysqli_real_escape_string($connection,$_POST['first_name']);
$MIDDLE_NAME = mysqli_real_escape_string($connection,$_POST['middle_name']);
$LAST_NAME = mysqli_real_escape_string($connection,$_POST['last_name']);
$CONTACT_NO = trim(mysqli_real_escape_string($connection,$_POST['contact_no']));
$ADDRESS = mysqli_real_escape_string($connection,$_POST['address']);
$ACTIVATION_CODE = md5($USERNAME.(rand(0,1000)));
$USER_TYPE = "ADMIN";
$stm->execute();
}
?>
Hope you can help me.
Is this a typo, or you need to correct that? Correct mysli to mysqli
$stm = $mysli_link->prepare("INSERT....
Also, bind_param accepts one more parameter types, which is the string with one or more characters defined as per information below. This parameter is required if any of the parameters size exceeds max_allowed_packet.
The types is defined as follows:-
types
A string that contains one or more characters which specify the types for the corresponding bind variables:
Type specification chars
Character Description
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
So concatenate each letter for variable types in sequence