Folks,
This Error handling Subject is doing my head in.
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);
}
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.