/    Sign up×
Community /Pin to ProfileBookmark

Which Type Of Prepared Statement Error Checking Should I Use ?

Php Buddies,

For my learning purpose, do mention what is wrong with the sets you do not recommend and what kind of problems I would face should I ever use them out of mistake.

Q1. sets regarding mysqli_stmt_execute(). Which set to keep ?

SET 1

““
if(mysqli_stmt_execute($stmt) === FALSE)
{
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
die;
}
““

SET 2

““
$stmt_execute = mysqli_stmt_execute($stmt);

if($stmt_execute === FALSE)
{
echo __LINE__;
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
die;
}
““

Q2. 2 sets regarding mysqli_stmt_bind_result(). Which set to keep ?

SET 1

““
if($bind_result = mysqli_stmt_bind_result($stmt,$page_url,$link_anchor_text,$page_description,$keyphrase,$keywords));
{
echo __LINE__;
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
die;
}
““

SET 2

““
$bind_result = mysqli_stmt_bind_result($stmt,$page_url,$link_anchor_text,$page_description,$keyphrase,$keywords);

if($bind_result === FALSE)
{
echo __LINE__;
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
die;
}
““

Q3. 2 sets regarding mysqli_stmt_fetch(). Which set to keep ?

SET 1

““
if($stmt_fetch = mysqli_stmt_fetch($stmt))
{
echo __LINE__;
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
die;
}
““

SET 2

““
$stmt_fetch = mysqli_stmt_fetch($stmt);

if($stmt_fetch === FALSE)
{
echo __LINE__;
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
die;
}
““

Anything else I should know, you are welcome to mention.

Thanks

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 30.2020 — In things like this...
<i>
</i>if($bind_result = mysqli_stmt_bind_result($stmt,$page_url,$link_anchor_text,$page_description,$keyphrase,$keywords));

...if you are not going to use $bind_result anywhere else, then just get rid of the $bind_result = part.
Copy linkTweet thisAlerts:
@developer_webauthorJul 31.2020 — @NogDog#1621521

Guess what NogDog ?

On Q2, on the bind_result(), you suggested I use SET 1 over SET 2. But SET 1 throws error. SET 2 doesn't.

Shows this error in line 47:

**47Error: . Error: 0.**

Out of the 2 SETS, I commented-out the one that shows the error. Look:
<i>
</i>/*
//1ST SET OUT OF 2 SETS REGARDING mysqli_stmt_bind_result. QB. WHICH SET TO KEEP ?
if($bind_result = mysqli_stmt_bind_result($stmt,$page_url,$link_anchor_text,$page_description,$keyphrase,$keywords)); //SHOULD I KEEP THIS IF OR THE IF BEFORE THIS ONE ?
{
echo __LINE__;
printf("Error: %s.n", mysqli_stmt_error($stmt));
printf("Error: %d.n", mysqli_stmt_errno($stmt));
die;
}
*/
//2ND SET OUT OF 2 SETS REGARDING mysqli_stmt_bind_result. QB. WHICH SET TO KEEP ?
$bind_result = mysqli_stmt_bind_result($stmt,$page_url,$link_anchor_text,$page_description,$keyphrase,$keywords);
if($bind_result === FALSE) //Q7. SHOULD I KEEP THIS IF OR THE NEXT IF AFTER THIS ONE ?
{
echo __LINE__;
printf("Error: %s.n", mysqli_stmt_error($stmt));
printf("Error: %d.n", mysqli_stmt_errno($stmt));
die;
}


On Q3, on stmt_fetch(), I get same error on SET 1. Again, i commented-out the SET that shows the error .....
<i>
</i>/*
//1ST SET OUT OF 2 SETS REGARDING mysqli_stmt_fetch. QC. WHICH SET TO KEEP ?
if($stmt_fetch = mysqli_stmt_fetch($stmt)) //Q11. SHOULD I KEEP THIS IF OR THE IF THAT COMES BEFORE THIS IF ?
{
echo __LINE__;
printf("Error: %s.n", mysqli_stmt_error($stmt));
printf("Error: %d.n", mysqli_stmt_errno($stmt));
die;
}
*/
//2ND SET OUT OF 2 SETS REGARDING mysqli_stmt_fetch. QC. WHICH SET TO KEEP ?
$stmt_fetch = mysqli_stmt_fetch($stmt);
if($stmt_fetch === FALSE) //Q11. SHOULD I KEEP THIS IF OR THE IF THAT COMES AFTER THIS IF ?
{
echo __LINE__;
printf("Error: %s.n", mysqli_stmt_error($stmt));
printf("Error: %d.n", mysqli_stmt_errno($stmt));
die;
}


It seems the long versions show error but not the short versions! Why is that ?

What I have learnt from the above is that the long version shows errors:

if($bind_result = mysqli_stmt_bind_result())


if($stmt_fetch = mysqli_stmt_fetch($stmt))


And so, have to use short versions as they don't show error.

Long version means checking against the variable (who's value is the function). Short version means checking directly against the function.

$bind_result = mysqli_stmt_bind_result($stmt,$page_url,$link_anchor_text,$page_description,$keyphrase,$keywords);
if($bind_result === FALSE)


$stmt_fetch = mysqli_stmt_fetch($stmt);

if($stmt_fetch === FALSE)


But wait! How come this following long version works aswell the short version ? What is the mystery ? I definitely need answer to this one!

if(mysqli_stmt_execute($stmt) === FALSE)
{
echo __LINE__;
printf("Error: %s.n", mysqli_stmt_error($stmt));
printf("Error: %d.n", mysqli_stmt_errno($stmt));
die;
}



$stmt_execute = mysqli_stmt_execute($stmt);

if($stmt_execute === FALSE)
{
echo __LINE__;
printf("Error: %s.n", mysqli_stmt_error($stmt));
printf("Error: %d.n", mysqli_stmt_errno($stmt));
die;
}


How come both SETS (both short & long versions) work on this final one ? Any ideas ?
Copy linkTweet thisAlerts:
@developer_webauthorAug 26.2020 — No ideas ?
Copy linkTweet thisAlerts:
@developer_webauthorOct 04.2020 — I need my previous post answered.
Copy linkTweet thisAlerts:
@criterion9Oct 06.2020 — Why wouldn't you simplify instead of assigning variables you aren't going to use?
<i>
</i>if(!mysqli_stmt_bind_result($stmt,$page_url,$link_anchor_text,$page_description,$keyphrase,$keywords)){
//it was false
}
×

Success!

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