Folks,
I need to get the code to echo “Sorry! Something went wrong!” if for some reason fetching data from db was unsuccessful.
Here are some ways I am doing it. 3 samples.
NOTE the IFs on each sample. That is where the 3 samples differ.
Tell me which one I should stick to and why that one over the others.
I ranked them according to favourite ….
What is your ranking if all 3 would work and is fine ?
1.
[code]
<?php
$server = ‘localhost’;
$user = ‘root’;
$password = ”;
$database = ‘bruteforce’;
$conn = mysqli_connect(“$server”,”$user”,”$password”,”$database”);
$keywords = ‘keyword’;
$query = ‘SELECT COUNT(id) 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($result = mysqli_stmt_bind_result($stmt,$row_count))
{
mysqli_stmt_fetch($stmt);
echo ‘Result Found: ‘ .$row_count; echo ‘<br>’;
}
else
{
echo ‘Sorry! Something went wrong!’; echo ‘<br>’;
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
?>
2.
[code]
<?php
$server = ‘localhost’;
$user = ‘root’;
$password = ”;
$database = ‘bruteforce’;
$conn = mysqli_connect(“$server”,”$user”,”$password”,”$database”);
$keywords = ‘keyword’;
$query = ‘SELECT COUNT(id) from links WHERE keywords = ?’;
$stmt = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,’s’,$keywords);
if(mysqli_stmt_execute($stmt))
{
$result = mysqli_stmt_bind_result($stmt,$row_count);
mysqli_stmt_fetch($stmt);
echo ‘Result Found: ‘ .$row_count; echo ‘<br>’;
}
else
{
echo ‘Sorry! Something went wrong!’; echo ‘<br>’;
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
?>
3.
[code]
<?php
$server = ‘localhost’;
$user = ‘root’;
$password = ”;
$database = ‘bruteforce’;
$conn = mysqli_connect(“$server”,”$user”,”$password”,”$database”);
$keywords = ‘keyword’;
$query = ‘SELECT COUNT(id) 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);
$result = mysqli_stmt_bind_result($stmt,$row_count);
if(mysqli_stmt_fetch($stmt))
{
echo ‘Result Found: ‘ .$row_count; echo ‘<br>’;
}
else
{
echo ‘Sorry! Something went wrong!’; echo ‘<br>’;
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
?>