/    Sign up×
Community /Pin to ProfileBookmark

Is This How You Check If Data Fetching Successful Or Not ?

Hi,

Is this code sound now ? I got a little help.

[code]

ini_set(‘display_errors’,1);
ini_set(‘display_startup_errors’,1);
error_reporting(E_ALL);

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

$conn = mysqli_connect(“localhost”,”root”,””,”gulf”); //mysqli_connect(“server”,”user”,”password”,”db”);

$input_1 = ‘here’; //keyword
$input_2 = ‘search here’; //keyphrase

$sql = “SELECT domain from links WHERE keyword = ? OR keyphrase = ?”;

$stmt = mysqli_stmt_init($conn);

if(mysqli_stmt_prepare($stmt,$sql))
{
mysqli_stmt_bind_param($stmt,”ss”,$input_1,$input_2);
mysqli_stmt_execute($stmt);
if(!mysqli_stmt_bind_result($stmt,$output_1)) //mysqli_bind_result() need to be written once. Do not put inside any loop. https://stackoverflow.com/questions/67252495/on-which-line-to-check-if-data-fetching-successful-or-not-when-using-mysqli-stmt/67252635#67252635
{
die(‘Unable to securely query the database!’);
}
else
{
//Below code source is: https://stackoverflow.com/questions/67252495/on-which-line-to-check-if-data-fetching-successful-or-not-when-using-mysqli-stmt/67252635#67252635
$fetchResult = null;
while ($fetchResult = mysqli_stmt_fetch($stmt))
{
echo “$output_1<br>”;
}
/* dont forget to use 3 equal signs to also compare variable type */
/* null == false values are considered the same */
/* null === false this also compare variable types, here types are not the same */
if ($fetchResult === false)
{
die(“mysqli_stmt_fetch failed !”);
}
}
}
else
{
echo ‘Mysqli Error: ‘ .mysqli_stmt_error();
echo ‘<br>’;
echo ‘Mysqli Error No: ‘ .mysqli_stmt_errno();
}

mysqli_stmt_close($stmt);
mysqli_close($conn);
[/code]

I need to put checking points at appropriate places./lines and not check in wrong lines if data fetching was successful or not.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 16.2022 — > @novice2022#1644074 mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

With strict set, I believe you should be able to catch exceptions for any database errors, so you could wrap all the code that will include mysqli stuff withing a try/catch block:
[code=php]
try {
// lots of amazing code here...
}
catch(mysqli_sql_exception $e) {
error_log("MySQLi exception:n".var_export($e));
die("There was a database error. Details have been logged for the site administrator.");
// for debugging, you might instead do:
// die("<pre>".var_export($e)."</pre>");
}
catch(Exception $e) {
error_log("Exception:n".var_export($e));
die("there was an unexpected error. Details have been logged for the site administrator.")
}
[/code]

I'm not sure if all types of errors can be caught that way, but it could mean you don't have to test every mysqli function call for a false result. (PDO works that way, but this isn't PDO. :( )
Copy linkTweet thisAlerts:
@novice2022authorMay 16.2022 — @NogDog#1644075

Mmm. I just finished reading these:

**On Which Line To Check If Data Fetching Successful Or Not When Using mysqli_stmt_bind_result?**

https://stackoverflow.com/questions/67252495/on-which-line-to-check-if-data-fetching-successful-or-not-when-using-mysqli-stmt/67252635#67252635

**How to deal with mysqli problems? mysqli_fetch_array(): Argument #1 must be of type mysqli_result:**

https://stackoverflow.com/questions/22662488/how-to-deal-with-mysqli-problems-mysqli-fetch-array-argument-1-must-be-of-t/22662582#22662582

**PHP error reporting**

https://phpdelusions.net/mysqli/error_reporting

**How to report errors in mysqli**

https://phpdelusions.net/mysqli/error_reporting

And what I understood from all these readings is that I shouldn't even bother checking for prepared statemnts or mysqli function errors.

So, I should just forget any of these lines:

1.
<i>
</i>if(**!**mysqli_stmt_bind_param())
{
echo mysqli_stmt_error();
die('Custom Error Message');
}


2.
<i>
</i>if(**!**mysqli_stmt_bind_result())
{
echo mysqli_stmt_error();
die('Custom Error Message');
}


3.
<i>
</i>if(**!**mysqli_stmt_get_result())
{
echo mysqli_stmt_error();
die('Custom Error Message');
}


4.
<i>
</i>if(**!**mysqli_stmt_fetch_result())
{
echo mysqli_stmt_error();
die('Custom Error Message');
}


5.
<i>
</i>if(!mysqli_stmt_fetch())
{
echo mysqli_stmt_error();
die('Custom Error Message');
}


6.
<i>
</i>if(!mysqli_fetch_array())
{
echo mysqli_stmt_error();
die('Custom Error Message');
}


7.
<i>
</i>if(!mysqli_stmt_execute())
{
echo mysqli_stmt_error();
die('Custom Error Message');
}


8.
<i>
</i>if(!mysqli_stmt_store_result())
{
echo mysqli_stmt_error();
die('Custom Error Message');
}


Am I right NogDog ?

If so, then why do all tutorials including the manual write like this ?
<i>
</i>if($stmt=mysqli_prepare($conn,$sql))
{
continue with prepared statement ....
}

Why we should check for mysqli here then ?

I got this question to you, the pdodelusion guy and all the tutorials that teach this code.
Copy linkTweet thisAlerts:
@novice2022authorMay 16.2022 — People,

Reading this:

https://phpdelusions.net/articles/error_reporting

Still confused about exception, handling, catch_all,, try.

What are they.

Exception ? Excption to what ?

Catch_all ? What is php trying to catch ? A tennis ball ?

Try ? Try what ? Is php trying tp get connected to db or try running a piece of code ?

I don't understand any of this.

Let me guess.

Phy tries running a block of code. You write this code under the 'try'.

If php encounters a stumbling block in the try script flow, then if there is an exception block then what does it do ? What is php meant to do while running this block ? Ignore the error and keep running the script flow ?

And if there is a handler block then what does it do ? What does the handler block code meant to axtually do here ?
Copy linkTweet thisAlerts:
@NogDogMay 16.2022 — I'll start you out with the official description of Exceptions: https://www.php.net/exceptions

This is not unique to PHP, and many other languages use them in similar ways. Basically, they're for things that shouldn't happen, but just in case... :) In other words, you don't normally use them to see if the user left a required field blank, and you wouldn't use them for detecting that a DB query returned zero rows. You may want to detect those situations and handle them accordingly, but they're not a sign that something is "broken" in your code (or in the web server, etc.).

If anything "throws" an exception, if it's not caught within a try/catch[/finally] block somewhere in your code, then it just bubbles up to the top, so to speak, and outputs a fatal error (with the word "Exception" somewhere in it.) Using try/catch lets you control how things are handled when exceptions occur.

Anyway, your description is basically correct: The code within the try block runs normally, unless something goes wrong that throws an exception. At that point, the processing jumps straight to the first catch block it finds that matches the exception type that was thrown, at which point processing continues as defined within that specific catch block. (Any code within the try block after the point where the exception was thrown does not get processed at all...which is where an optional "finally" block might be desired to sort of clean up the mess, so to speak.)
×

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.20,
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,
)...