/    Sign up×
Community /Pin to ProfileBookmark

foreach, while or both?

in the following peice of code is a pdo sql query. im wondering which should be used to work with the data just to echo it out say, a foreach loop or a while loop. if both are possible can you please give me an example of each. thanks

[CODE]
$sql = “SELECT * FROM table WHERE column1 = :foo and column2 – :bar”;

$stmt = $connect->prepare($sql);

$result = $stmt->execute(array(
‘:foo’ = $_GET[“foo”],
‘:bar’ = $_GET[“bar”}
));
[/CODE]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 14.2019 — <i>
</i>foreach($stmt as $row) { . . . }
// or
while($row = $stmt-&gt;fetch()) { . . . }

The first is a bit "cleaner", the second more flexible, in that you can pass any of the 3 optional parameters to it as desired (see https://www.php.net/manual/en/pdostatement.fetch.php ).
Copy linkTweet thisAlerts:
@coleioauthorApr 14.2019 — This stuff hurts my head lol. Thanks for all your help mate
Copy linkTweet thisAlerts:
@ginerjmApr 15.2019 — I prefer to use the while loop since it makes it very apparent what is being processed at that point, when one is unfamiliar with the script. And, as pointed out by NogDog, it allows you to alter how your retrieve the query results (see the manual under 'fetch').
Copy linkTweet thisAlerts:
@coleioauthorApr 15.2019 — nogdog shouldnt that be

[CODE]
foreach ($result as $row)
[/CODE]


??
Copy linkTweet thisAlerts:
@NogDogApr 15.2019 — @coleio#1602814

Nope: PDOStatement::execute() returns a Boolean, not a PDOStatement object, so there's nothing to loop on. (Would be nice if it did for when you want to chain methods, but... 🤷‍♂️ )
Copy linkTweet thisAlerts:
@coleioauthorApr 15.2019 — So the result variable is pointless?
Copy linkTweet thisAlerts:
@ginerjmApr 15.2019 — IF you read the manual - you will find that the result of the execute call yields a Boolean telling you whether or not your statement executed successfully. So - it is not pointless. Sometimes your script may call for a need to know that result so it comes in handy.
Copy linkTweet thisAlerts:
@NogDogApr 15.2019 — Yep...code defensively. Assume that anything that can fail will fail, so figure out what should happen if it does. If you turn on Exceptions for any PDO error (see https://www.php.net/manual/en/pdo.setattribute.php ), you could use a try/catch block to catch any errors from PDO methods, limiting the number of if($something == false) checks you need to do.
×

Success!

Help @coleio 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.24,
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,
)...