/    Sign up×
Community /Pin to ProfileBookmark

Why The Extra WHILE Loop here ?

Hello,

Looking at this code in the following url, can anyone tell me if the WHILE loop is needed or not ?
(I am unable to paste the code here as I keep getting this silly error, “You do not have permission to do this”. whenever I try pasting codes. Check the img below).

With or without this WHILE LOOP, I still get the same results. Result outputs are same.
So, why the unnecesary WHILE here ?

I got the code from this tutorial:
https://dev.mysql.com/doc/apis-php/en/apis-php-mysqli-stmt.get-result.html

It seems, when using the foreach you don’t need the WHILE Loop.
I am talking abut the foreach inside the WHILE Loop.

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmMay 16.2022 — remove any php tags and post the code. The forum doesn't like them or use <-? for a tag instead

<i>
</i>&lt;-?
echo "$xyz"
-&gt;
Copy linkTweet thisAlerts:
@novice2022authorMay 16.2022 — @ginerjm#1644059

<i>
</i>mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

$query = "SELECT Name, Population, Continent FROM Country WHERE Continent=? ORDER BY Name LIMIT 1";

$stmt = mysqli_prepare($link, $query);
mysqli_stmt_bind_param($stmt, "s", $continent);

$continentList= array('Europe', 'Africa', 'Asia', 'North America');

foreach ($continentList as $continent) {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
foreach ($row as $r) {
print "$r ";
}
print "n";
}
}


Thanks Gin! It worked!
Copy linkTweet thisAlerts:
@ginerjmMay 16.2022 — I do not (never have) use mysqli so I may be wrong here but you have to do the bind inside the loop, not just one time before the loop. In fact since I don't see where $continent is defined at the bind point, if you had error checking turned on you would probably get some message.

Now where is the un-needed while?

And - why the multiple queries? Why not get all continents at once and order by both continent and name and then do an html table output?
Copy linkTweet thisAlerts:
@NogDogMay 16.2022 — To answer your original question, the while loop is needed to loop through each result row returned by the database query. If you're sure you would never get (or at least use) more than one row, I suppose it could be done without the while loop -- but it doesn't hurt anything to have it.
Copy linkTweet thisAlerts:
@novice2022authorMay 16.2022 — @ginerjm#1644062

So you deem this tutorial code wrong then ?

https://dev.mysql.com/doc/apis-php/en/apis-php-mysqli-stmt.get-result.html
Copy linkTweet thisAlerts:
@ginerjmMay 16.2022 — I only look at code that is posted. I don't click on 'things'. If you have an issue with a certain part of the code why not cut/paste it here for us (chickens) to review?
Copy linkTweet thisAlerts:
@NogDogMay 16.2022 — > @novice2022#1644069 So you deem this tutorial code wrong then ?

I don't think it's "wrong", because it binds the variable $continent, and then the foreach loop is setting that variable on each iteration. That being said, I personally would never use that approach for that example, as I could get the same results via a single query. 🤷
Copy linkTweet thisAlerts:
@ginerjmMay 16.2022 — I kinda assumed that the bind occurred at the time it is called. Thanks for the clarification
Copy linkTweet thisAlerts:
@NogDogMay 16.2022 — > @ginerjm#1644072 I kinda assumed that the bind occurred at the time it is called. Thanks for the clarification

Assuming I'm right. :) As you stated, I _much_ prefer using PDO, and that's all I've been using for several years now. 👍️
Copy linkTweet thisAlerts:
@novice2022authorMay 16.2022 — @NogDog#1644071

You mean something like this without using foreach twice:
<i>
</i>
$conn = mysqli_connect("localhost","root","","gulp"); //mysqli_connect("server","user","password","db");

$inputs = array('gulp','ebrute');

$sql = "SELECT id,email from users WHERE username = ?";

$stmt = mysqli_stmt_init($conn);

if(mysqli_stmt_prepare($stmt,$sql))
{
mysqli_stmt_bind_param($stmt,"s",$input);

<i> </i>foreach($inputs AS $input)
<i> </i>{
<i> </i> mysqli_stmt_execute($stmt);
<i> </i> $result = mysqli_stmt_get_result($stmt);
<i> </i>
<i> </i> while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) //Do not do: while($row=mysqli_fetch_array($result,MYSQLI_BOTH)) as it outputs each result twice.
<i> </i> {
<i> </i> echo $id = $row['id'];
<i> </i> echo $email = $row['email'];
<i> </i> }
<i> </i>}
}
else
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}

mysqli_stmt_close($stmt);
mysqli_close($conn);



Yes ?

If not, then atleast the above code is flawless. Yes ?
Copy linkTweet thisAlerts:
@novice2022authorMay 16.2022 — @ginerjm#1644062

Tryout the above code.

https://forum.webdeveloper.com/d/399838-why-the-extra-while-loop-here/3

You will see it works. Now erase the WHILE loop and you will see you get the same results. The foreach substitutes the WHILE in my opinion and so you get the same results. The while loop is not needed here, That's what I think but I need confirmation from you.
Copy linkTweet thisAlerts:
@ginerjmMay 16.2022 — I have no need to do anything with that code. If you're happy, I am
Copy linkTweet thisAlerts:
@sportstototvMay 20.2022 — Hurrah! In the end I got a webpage from where I be capable

of genuinely get useful information regarding my study and knowledge.

[스포츠중계](https://www.sportstototv.com)
Copy linkTweet thisAlerts:
@sportstototvMay 20.2022 — [배트맨토토](https://www.betmantoto.pro) Great article! All the articles you have, they enjoy reading and learning a lot. Your article is very helpful for me. I hope you will continue to write such good articles as well
Copy linkTweet thisAlerts:
@sportstototvMay 20.2022 — Keep up the good writing. please visit our beautiful website, spread the love, thankyou!

[스포츠토토](https://www.toto365.pro)
Copy linkTweet thisAlerts:
@sportstototvMay 20.2022 — [카지노사이트](https://www.casinositehome.com) I have never seen this type of information before. Thanks for sharing this. Please also visit !
×

Success!

Help @novice2022 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...