/    Sign up×
Community /Pin to ProfileBookmark

How To Check If Fetching Data Failed Or Not For Technical Reasons ?

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);
[/code]

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
[/code]

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);
?>
[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorMay 11.2021 — @Sempervivum

Do you mind answering my Q2 above ?
Copy linkTweet thisAlerts:
@developer_webauthorMay 11.2021 — @NogDog,

Do you mind answering my Q1 on my oop ?
Copy linkTweet thisAlerts:
@SempervivumMay 11.2021 — @developer_web#1631507 PHP and database ist not in focus for me and additionally I prefer using PDO instead of mysqli. Therefore I'm not able to answer your questions.
Copy linkTweet thisAlerts:
@developer_webauthorMay 12.2021 — @tracknut

You got experience in mysqli ? Can you answer my two questions on my original post ?

Thanks!
Copy linkTweet thisAlerts:
@developer_webauthorJul 18.2021 — @NogDog,

I don't know anyone better than you who can answer my op.
×

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