When using Prepared Statements (Prep Stmts), to fetch rows from Mysql, we can use:
- mysq_stmt_bind_result()
- mysqli_stmt_get_result().
Let's talk about the latter.
When fetching multiple rows, I use either of these:
- mysqli_fetch_array()
- mysqli_fetch_assoc().
Like so ....
NOTE the WHILE Loops on both:
$query = 'SELECT * from links WHERE keywords = ? ORDER BY id';
$stmt = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,'s',$keywords);
mysqli_stmt_execute($stmt);
if($result = mysqli_stmt_get_result($stmt))
{
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo 'Id: ' .$id = $row['id']; echo '<br>';
echo 'Domain: ' .$id = $row['id']; echo '<br>';
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
}
else
{
echo 'No Result!';
}
}
$keywords = 'keywords';
$query = 'SELECT * from links WHERE keywords = ? ORDER BY id';
$stmt = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,'s',$keywords);
mysqli_stmt_execute($stmt);
if($result = mysqli_stmt_get_result($stmt))
{
while($row = mysqli_fetch_assoc($result))
{
echo 'Id: ' .$id = $row['id']; echo '<br>';
echo 'Domain: ' .$id = $row['id']; echo '<br>';
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
}
else
{
echo 'No Result!';
}
}
My question is, none of these 2:
- mysqli_fetch_array()
- mysqli_fetch_assoc().
got the "stmt" on them.
Should not they be:
- mysqli_stmt_fetch_array()
- mysqli_stmt_fetch_assoc().
Q1. Yes or No ?
Q2. When do we use the:
Maybe like so ? ....
Again, note the WHILE loop on both following codes.
$query = 'SELECT * from links WHERE keywords = ? ORDER BY id';
$stmt = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,'s',$keywords);
mysqli_stmt_execute($stmt);
if($result = mysqli_stmt_get_result($stmt))
{
while($row = mysqli_stmt_fetch($stmt))
Is this following valid or not ? Yes or No ? Did I miss anything out ?
<?php
$server = 'localhost';
$user = 'root';
$password = '';
$database = 'brute';
$conn = mysqli_connect("$server","$user","$password","$database");
$keywords = 'keywords';
$query = 'SELECT * from links WHERE keywords = ? ORDER BY id';
$stmt = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,'s',$keywords);
mysqli_stmt_execute($stmt);
if($result = mysqli_stmt_get_result($stmt))
{
while($row = mysqli_stmt_fetch($stmt)) //IS THIS LINE AND ALL LINES BELOW VALID OR NOT ?
{
echo 'Id: ' .$id = $row['id']; echo '<br>';
echo 'Domain: ' .$id = $row['id']; echo '<br>';
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
}
else
{
echo 'No Result!';
}
}
die;
?>
NOTE: I not into oop yet and so don't show me any oop code because I won't understand. Ok ?
Q3.
Based on this:
https://www.php.net/manual/en/mysqli-stmt.fetch.php
it seems you use the:
mysqli_stmt_fetch()
with the:
mysqli_stmt_bind_result().
So, guessing you don;t use it with the:
mysqli_stmt_get_result().
I actually used to know these things but forgotten. The
"mysqli_stmt_" functions are messy and so been told many times to get into pdo. I know, I know. So don't repeat.