mysqli prepared statement with fetch_assoc
From the PHP website page for mysqli->prepare (with emphasis added to the most relevant part):
Note:
The markers are legal only in certain places in SQL statements. For
example, they are allowed in the VALUES() list of an INSERT statement
(to specify column values for a row), or in a comparison with a column
in a WHERE clause to specify a comparison value.However, they are not allowed for identifiers (such as table or column
names), in the select list that names the columns to be returned by a
SELECT statement), or to specify both operands of a binary operator
such as the = equal sign. The latter restriction is necessary because
it would be impossible to determine the parameter type. In general,
parameters are legal only in Data Manipulation Language (DML)
statements, and not in Data Definition Language (DDL) statements.
Assuming you can get past that problem, your use of mysqli
is a little confused. You correctly bind your parameters and execute, but you've mixed up two different ways of getting at your results. Either
- Use
mysqli_stmt_get_result
to fetch the result set and then usemysqli_fetch_assoc
on that, or - Bind your results with
mysqli_stmt_bind_result
, and then usemysqli_stmt_fetch
to fetch the next set of results into your bound variables. (Usually you'd iterate over the results using something likewhile(mysqli_stmt_fetch($stmt)){ //do stuff here }
How can I use mysqli fetch_assoc() several times with prepared statements on same PHP page?
You can use mysqli_result::data_seek
to reset the result pointer after each usage so you can loop over the results multiple times. For example:
while($row = $result->fetch_assoc()){
...
}
$result->data_seek(0);
while($row = $result->fetch_assoc()){
...
}
prepared statements fetch_assoc php mysqli
You have an error in your script. You are using mysqli_fetch_assoc
while you have to use fetch()
. The error is here
while ($row = mysqli_fetch_assoc($vid_comment)){
So you should use instead
while ($fetchComments ->fetch()) {
echo $vid_comment
}
You can check documentation here
PHP Mysqli prepared statement select fetch_assoc gives empty result
Your result is a single record in an array. When you do foreach ($json as $value)
, $value
will have 511, User Name, Wagirigoingi, etc. At some point, $value
will have the value corresponding to THMPATH in the $json
array, but at no time will $value
be an array, so $value['THMPATH']
is never going to be valid.
What you presumably want is to just eliminate the foreach
loop entirely.
Unable to Fetch_Assoc() of Prepared Statement in PHP, If Fetch is Used num_rows becomes Zero
Your first issue is you are not getting the result, I mean you're missing mysqli_stmt::get_result.
Your code is supposed to be
$user->bind_param("ss", $username, $pass);
$user->execute();
$result = $user->get_result();
if($result->num_rows === 1){
$row = $result->fetch_assoc()
$_SESSION['name']=$row['username'];
header("Location: admin.php");
}else{
echo "Invalid Username and Password";
}
Why fetching if no rows are returned right?
And you are overriding the session here
$_SESSION['name']=$row['username'];
$_SESSION['name']=true;
The last session $_SESSION['name']
is overriding the first one.
Related Topics
Get_Result() Doesn't Work Even MySQLnd Is Enabled
How to Inject a Repository into a Service in Symfony
How to Get Open Graph Protocol of a Webpage by PHP
Undefined Index While Uploading File
How to Debug Why Simplest MySQL Query Returns False
What Does New Self(); Mean in PHP
How to Add 24 Hours to a Unix Timestamp in PHP
Keeping Url Parameters During Pagination
How to Get &Curren to Display Literally, Not as an HTML Entity
How to Run Django and PHP Together on One Apache Server
How Would I Implement a Simple Site Search with PHP and MySQL
Stop Data Inserting into a Database Twice