Victor Kimura
asked on
php mysqli - getting results with for loop
Hi,
I'm just wondering how I can retrieve data using mysqli using a for loop and foreach loop rather than using a while statement. I heard or read somewhere that while is a little slower.
I have this bit of code:
/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT name, email FROM freecd WHERE email LIKE ? LIMIT 5")) {
$stmt->bind_param("s", $code);
$code = "vkim%";
$stmt->execute();
/* bind variables to prepared statement */
$stmt->bind_result($col1, $col2);
/* fetch values */
while ($stmt->fetch()) {
printf("%s %s\n", $col1, $col2);
}
/* close statement */
$stmt->close();
} else {
echo $mysqli->error;
}
Thanks,
vkimura
I'm just wondering how I can retrieve data using mysqli using a for loop and foreach loop rather than using a while statement. I heard or read somewhere that while is a little slower.
I have this bit of code:
/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT name, email FROM freecd WHERE email LIKE ? LIMIT 5")) {
$stmt->bind_param("s", $code);
$code = "vkim%";
$stmt->execute();
/* bind variables to prepared statement */
$stmt->bind_result($col1, $col2);
/* fetch values */
while ($stmt->fetch()) {
printf("%s %s\n", $col1, $col2);
}
/* close statement */
$stmt->close();
} else {
echo $mysqli->error;
}
Thanks,
vkimura
The "while" is not slower. Use it!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.