/    Sign up×
Community /Pin to ProfileBookmark

How To Add Error Handling: Exception ?

Folks,

This Error handling Subject is doing my head in.
https://www.php.net/manual/en/language.exceptions.php

You know, Exceptions, Try, Catchall, Block or what not.
Need a layman’s code sample to learn from you folks.
So, here is my code below. Imagine data fetching failed. Now, let’s do the error handling here to do Exceptions and the such.

[code]
$server = ‘localhost’;
$user = ‘root’;
$password = ”;
$database = ‘brute’;

$conn = mysqli_connect(“$server”,”$user”,”$password”,”$database”);

$keywords = ‘keyword’;

$query = ‘SELECT id,domain from links WHERE keywords = ?’;
$stmt = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,’s’,$keywords);
mysqli_stmt_execute($stmt))
if(!mysqli_stmt_bind_result($stmt,$id,$domain))
{
die(‘mysqli_stmt_bind_result() Failed!’);
}
else
{
while($fetch_result = mysqli_stmt_fetch($stmt))
{
echo ‘Id: ‘ .$id; echo ‘<br>’;
echo ‘Domain: ‘ .$domain; echo ‘<br>’;
}
if($fetch_result === FALSE)
{
die(‘mysqli_stmt_fetch() Failed!’);
}
}
}
else
{
echo ‘Sorry! Something went wrong. Try again later.’;
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
[/code]

Do show me a very tiny and simple Exception code sample (for a beginner) and anything else that is necessary. The try, Block, Catchall or whatever.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorApr 27.2021 — @Sempervivum

Care to chime in ?
Copy linkTweet thisAlerts:
@NogDogApr 27.2021 — For all the MySQLi stuff, you can use mysqli_report() to have mysqli errors generate exceptions.
[code=php]
<?php

// this turns on MySQLi exceptions:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// now you should be able to catch exceptions from MySQLi if something fails
try {
$server = 'localhost';
$user = 'root';
$password = '';
$database = 'brute';
$conn = mysqli_connect("$server","$user","$password","$database");
$keywords = 'keyword';
$query = 'SELECT id,domain from links WHERE keywords = ?';
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$query);
mysqli_stmt_bind_param($stmt,'s',$keywords);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt,$id,$domain);
while($fetch_result = mysqli_stmt_fetch($stmt)) {
echo 'Id: ' .$id; echo '<br>';
echo 'Domain: ' .$domain; echo '<br>';
}
} catch (mysqli_sql_exception $e) {
error_log("DATABASE ERROR:n".$e->getMessage());
echo "<p class='error'>Sorry, there was a database error. Details have been logged.</p>";
} catch (Exception $e) { // just in case
error_log("EXCEPTION:n".$e->getMessage());
echo "<p class='error'>Sorry, there was an unexpected error. Details have been logged.</p>";
} finally { // probably not really necessary in this example
if(!empty($stmt)) { mysqli_stmt_close($stmt); }
if(!empty($conn)) { mysqli_close($conn); }
}
[/code]
×

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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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