LinuxOwner
LinuxOwner

Reputation: 35

Input regarding this PHP script to query a mysql database?

I was using this script and everything was great except for the fact that when the actual search was executed and "No Results" was the answer, I wanted the script to display such.

When doing research to see where the FAIL was I discovered that I should be using MySQLi. I have been at this script for 2 days and I seem to be getting further instead of closer. A little help here fellas?

What I am using:

    if(empty($_GET['query'])){
header ("Location: /same_page");
}
else{
//connect
include 'connection_script.php';

//Get the "Term" from the search box
$query=mysql_real_escape_string($_GET['query']);

$page_str = "SELECT * FROM $tblname WHERE name like '%$query%' or clan like '%$query%'";

$page_query = mysqli_query($con,$page_str)or die(mysql_error($con));

while($page_result = mysqli_fetch_assoc($page_query)){$datarow .= " <ul>
<li>Banned player : <a target='_blank' href=\"http://path/tosomething/here=" . $page_result[name] . " \">" . $page_result[name] . "</a></li>
<li>Clan Name : " . $page_result[clan] . "</li> 
<li>Reason : " . $page_result[reason] . "</li>
<li>Posted By : " . $page_result[moderator] . "</li>
<li>Date & Time : " . $page_result[dateandtime] . "</li>
<li>Evidence : <a target='_blank' href=\"$page_result[evidence]\">Here</a></li>
</ul><br />";
}

echo $datarow;
echo "<br />";

include 'dbclose.php';
}

mysql_close($con);

Upvotes: 2

Views: 105

Answers (2)

David Sigley
David Sigley

Reputation: 1198

Just add a condition to display something if your query returns 0

      if(empty($_GET['query'])){
header ("Location: /same_page");
}
else{
//connect
include 'connection_script.php';

//Get the "Term" from the search box
$query=mysql_real_escape_string($_GET['query']);

$page_str = "SELECT * FROM $tblname WHERE name like '%$query%' or clan like '%$query%'";

$page_query = mysqli_query($con,$page_str)or die(mysql_error($con));

if (mysqli_num_rows($page_query) > 0){

while($page_result = mysqli_fetch_assoc($page_query)){$datarow .= " <ul>
<li>Banned player : <a target='_blank' href=\"http://path/tosomething/here=" . $page_result[name] . " \">" . $page_result[name] . "</a></li>
<li>Clan Name : " . $page_result[clan] . "</li> 
<li>Reason : " . $page_result[reason] . "</li>
<li>Posted By : " . $page_result[moderator] . "</li>
<li>Date & Time : " . $page_result[dateandtime] . "</li>
<li>Evidence : <a target='_blank' href=\"$page_result[evidence]\">Here</a></li>
</ul><br />";
}

echo $datarow;
echo "<br />";
} else {
echo 'Your search returned 0 results';
}

include 'dbclose.php';
}

mysql_close($con);

Upvotes: 0

Zweer
Zweer

Reputation: 124

You can retrieve the count of the rows with:

mysqli_num_rows($page_query);

Simply verify that it is >0 to chose what to display, the error message or the results

Upvotes: 1

Related Questions