Folks,
I am not trying to check if there are search results or not. To search for search results you usually use the mysqli_stmt_num_rows(). But I use the COUNT function. Like so:
[code]
$query = “SELECT COUNT(id) From links WHERE keyword = ?”;
$stmt = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,’s’,$keyword);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt,$row_count);
if($result = mysqli_stmt_fetch($stmt))
{
echo ‘Row Count: ‘ .$row_count; echo ‘<br>’;
}
else
{
echo ‘Record fetching failed!’;
echo ‘Error: ‘ .mysqli_stmt_error($conn);
echo ‘Error: ‘ .mysqli_stmt_errno($conn);
}
mysqli_stmt_close($stmt);
}
else
{
echo ‘Search Preparation Failed!’;
}
mysqli_close($conn);
And so, I know how to check whether there are matching rows or not for your keyword search on my mysql database.
What I am trying to learn is to check if fetching data from my mysql database was successful or not. And I am spoilt for choice how to check that. So, how should I check ?
Look at these experiments I did:
EXPERIMENT:
[code]
//if(!$col) //Output: Record fetching failed!Error: Error: 0
//if(!$col = mysqli_fetch_array($result)) //Output: Record fetching failed!Error: Error: 0
//if(!$col == mysqli_fetch_array($result)) //Output: Blank
//if(!$col === mysqli_fetch_array($result)) //Output: Blank
//if($col != mysqli_fetch_array($result)) //Output: Blank
//if($col !== mysqli_fetch_array($result)) //Output: Blank
//if($col !=== mysqli_fetch_array($result)) //Output: Parse error: syntax error, unexpected ‘=’ in …
//if(mysqli_fetch_array($result) == FALSE)//Output: Record fetching failed!Error: Error: 0
//if(mysqli_fetch_array($result) === FALSE) //Output: Blank
//if(!mysqli_fetch_array($result))//Output: Record fetching failed!Error: Error: 0
//if(mysqli_fetch_array($result) != FALSE) //Output: Blank
//if(mysqli_fetch_array($result) !== FALSE) //Output: Record fetching failed!Error: Error: 0—
//if(mysqli_fetch_array($result) !=== FALSE) //Output: Parse error: syntax error, unexpected ‘=’ in …
//if($col === FALSE) //Output: Blank
//if($col) //Output: Blank
Note the comments in the above code as they tell you what got echoed as output.
Q1.
Anyways, which IFs out of the 14 above are valid to check if data fetching was successful or not ?
Q2.
Can someone be kind enough to explain to me one by one what each of these 14 IFs mean ?
CONTEXT:
[code]
<?php
//5.
//MULTIPLE RECORDS FETCHING – mysqli_stmt_get_result(): mysqli_fetch_array().
//ERROR REPORTING.
ini_set(‘display_errors’,’1′);
ini_set(‘display_startup_errors’,’1′);
error_reporting(E_ALL);
//MYSQLI CONNECTION.
//BAREBONE TEMPLATE.
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$server = ‘localhost’;
$user = ‘root’;
$password = ”;
$database = ‘brute’;
if(!$conn = mysqli_connect(“$server”,”$user”,”$password”,”$database”))
{
echo ‘Mysqli Connection Error’ .mysqli_connect_error($conn);
echo ‘Mysqli Connection Error Number’ .mysqli_connect_errno($conn);
}
mysqli_set_charset($conn,’utf8mb4′);
//QUERY DATABASE.
$keyword = ‘keywords’;
$query = “SELECT id,date_and_time,domain,domain_email,ip,url,anchor,title,description,keyword,keyphrase From links WHERE keyword = ?”;
$stmt = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,’s’,$keyword);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while($col = mysqli_fetch_array($result,MYSQLI_NUM))
{
$id = $col[‘0’];
$date_and_time = $col[‘1’];
$domain = $col[‘2’];
$domain_email = $col[‘3’];
$ip = $col[‘4’];
$url = $col[‘5’];
$anchor = $col[‘6’];
$title = $col[‘7’];
$description = $col[‘8’];
$keyword = $col[‘9′];
$keyphrase = $col[’10’];
echo ‘Id: ‘ .$id; echo ‘<br>’;
echo ‘Date And Time: ‘ .$date_and_time; echo ‘<br>’;
echo ‘Domain: ‘ .$domain; echo ‘<br>’;
echo ‘Domain Email: ‘ .$domain_email; echo ‘<br>’;
echo ‘Ip: ‘ .$ip; echo ‘<br>’;
echo ‘Url: ‘ .$url; echo ‘<br>’;
echo ‘Anchor: ‘ .$anchor; echo ‘<br>’;
echo ‘Title: ‘ .$title; echo ‘<br>’;
echo ‘Description: ‘ .$description; echo ‘<br>’;
echo ‘Keyword: ‘ .$keyword; echo ‘<br>’;
echo ‘Keyphrase: ‘ .$keyphrase; echo ‘<br>’;
}
//WHICH ONE OF THESE 14 IFs BELOW ARE VALID ?
//if(!$col) //Output: Record fetching failed!Error: Error: 0
//if(!$col = mysqli_fetch_array($result)) //Output: Record fetching failed!Error: Error: 0
//if(!$col == mysqli_fetch_array($result)) //Output: Blank
//if(!$col === mysqli_fetch_array($result)) //Output: Blank
//if($col != mysqli_fetch_array($result)) //Output: Blank
//if($col !== mysqli_fetch_array($result)) //Output: Blank
//if($col !=== mysqli_fetch_array($result)) //Output: Parse error: syntax error, unexpected ‘=’ in …
//if(mysqli_fetch_array($result) == FALSE)//Output: Record fetching failed!Error: Error: 0
//if(mysqli_fetch_array($result) === FALSE) //Output: Blank
//if(!mysqli_fetch_array($result))//Output: Record fetching failed!Error: Error: 0
//if(mysqli_fetch_array($result) != FALSE) //Output: Blank
//if(mysqli_fetch_array($result) !== FALSE) //Output: Record fetching failed!Error: Error: 0—
//if(mysqli_fetch_array($result) !=== FALSE) //Output: Parse error: syntax error, unexpected ‘=’ in …
//if($col === FALSE) //Output: Blank
//if($col) //Output: Blank
{
echo ‘Record fetching failed!’;
echo ‘Error: ‘ .mysqli_stmt_error($stmt);
echo ‘Error: ‘ .mysqli_stmt_errno($stmt);
}
mysqli_stmt_close($stmt);
}
mysqli_close($conn);
?>