How do I add a No Results Found Message [duplicate] - php

This question already has answers here:
Check if a row exists using old mysql_* API
(6 answers)
Closed 6 years ago.
Trying to display a message, when there are no results found.
<?php
mysql_connect ("localhost", "root","") or die (mysql_error());
mysql_select_db ("test");
$term = $_POST['term'];
$sql = mysql_query("select * from testable where FName like '%$term%' or LName like '%$term%' or ID like '%$term%' ");
while ($row = mysql_fetch_array($sql)) {
echo '<br/>First Name:'.$row['FName'];
echo '<br/>Last Name:'.$row['LName'];
echo '<br/>Phone:'.$row['Phone'];
echo '<br/><br/>';
}
?>

Try this :
if(mysql_num_rows($sql) == 0)
{
echo 'No results';
}
else
{
while($row = mysql_fetch_array($sql))
{
//..
}
}

You can use mysql_num_rows() function, Which return the number of returned rows.
if( mysql_num_rows($sql) == 0) exit('No records found!');
passing the above condition meaning that there are some result, So you can loop through:
while ($row = mysql_fetch_array($sql)) {
// print whatever you want.
}

Related

Die if Data Not Exisits in php mysql table [duplicate]

This question already has answers here:
PHP mysql_num_rows die error
(3 answers)
Closed 5 years ago.
I'm getting an error message when the data is not available in data base table. I would like to stop the qry search without getting an error when the data is not available.
It works good if the data is available in table but not works while the data is not available. For an example, when i search this city name in my table the requested city's data's are not available in that table. During this time i'm getting an "Undefined variable" error.
<?php
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city' LIMIT 1";
$cityQryResult = mysql_query($cityQry);
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
?>
Check if mysql_num_rows gives you count more than 0, If count is 0 call die()
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city' LIMIT 1";
$cityQryResult = mysql_query($cityQry);
if(mysql_num_rows($cityQryResult) == 0)
{
die("No Data Exists");
} else {
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
}
Please try Below Code (Not Tested Yet) :
<?php
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city'";
$result = mysqli_query($cityQry);
// Mysql_num_row is counting table row
$count = mysqli_num_rows($result);
if($count > 0)
{
$cityQryResult = mysql_query($cityQry);
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
} else {
echo "No Data Exists";
}
?>

PHP check if record exist in SQL [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 6 years ago.
I am trying to check if record exist in SQL and get a return -> True or False.
It returns a notice:
Notice: Trying to get property of non-object in.... if($result->num_rows > 0)
$connection = new mysqli('localhost', 'user', 'pass','db_name');
$query = "SELECT * FROM order WHERE telephone = '".$telephone."' AND order_status_id='0' ";
$result = $connection->query($query);
if($result->num_rows > 0) {
echo 'found'; // The record(s) do exist
}
else{
echo 'not found'; // No record found
}
$connection->close();
ORDER is a mysql reserved word. Using it as field name, table name or whatever except ORDER BY fieldname requires using backticks:
SELECT * FROM `order` WHERE ....
Otherwise, you will get a query error, which will turn $result into boolean false.
Also, your code is vulnerable to sql-injection.
I advise you to use prepared statements.
You are checking the rows of the query object. The query object does not contain the rows.
What you want to do is something like
$data = $result->fetch_array();
if ($data->num_rows > 0)
{
//Rows exist
} else {
//No rows exist
}
It happens in case query do not injects object in your $result variable. You can make the following changes to proceed.
if(is_object($result) && $result->num_rows > 0) {
echo 'found'; // The record(s) do exist
} else{
echo 'not found'; // No record found
}
OR
if(!empty($result->num_rows)) {
echo 'found'; // The record(s) do exist
} else{
echo 'not found'; // No record found
}
Also keep the thing simple:
$query = "SELECT * FROM `order` WHERE telephone = '$telephone' AND order_status_id='0' ";
Use like this
$sqlReq = " SELECT * FROM order WHERE telephone = '".$telephone."' AND order_status_id='0' ";
$queryReq = mysql_query($sqlReq) or die(mysql_error());
if(mysql_num_rows($queryReq) > 0)
{
echo "found";
}else{
echo "not found";
}

Can not search in the database [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
Can anyone help me?
I trying to create a very basic search function for my website, but i have an error, and i can not search from my database, the error like in the image that i attached with.
It's ok when i enter the wrong name that is not exist on the database, but when i enter the name that is exist it will show the error like in the image, can anyone help me?
This is my php code:
<?php
require_once("../../include/admin/ad_ovhead.php");
require_once("../../lib/connection.php");
$output = '';
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);
$sql = "SELECT * FROM users WHERE firstname LIKE '%$searchq%' OR lastname LIKE '%$searchq%'";
$query= mysqli_query($conn, $sql) or die ("Can not search");
$count = mysqli_num_rows($query);
if($count == 0){
$output= 'There are no search results!';
}else{
while ($row = mysql_fetch_array($query)) {
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$id = $row['id'];
$output .= '<div> '.$firstname.' '.$lastname.' </div>';
}
}
}
?>
Use mysqli_num_rows() instead of mysql_num_rows()
$count = mysqli_num_rows($query);

Display first 50 characters only in php,mysql [duplicate]

This question already has answers here:
How do you pull first 100 characters of a string in PHP
(6 answers)
Closed 7 years ago.
I have two fields in database table, title and description. I am displaying tha data in php while loop.
My Code :
$sql = "select * from sightseeing";
$i = 1;
$res = mysql_query($sql, $conn);
if( mysql_num_rows($res) > 0) {
while($row = mysql_fetch_array($res))
{
echo "<tr>
<td>".$i++."</td>
<td>".$row["title"]."</td>
<td>".$row["description"]."</td>
</tr>";
}
}
I want to show only first 50 characters from description field. How to do that?
try this
$sql = "select * from sightseeing";
$i = 1;
$res = mysql_query($sql, $conn);
if( mysql_num_rows($res) > 0) {
while($row = mysql_fetch_array($res))
{
echo "<tr>
<td>".$i++."</td>
<td>".$row["title"]."</td>
<td>".substr($row['description'], 0, 50)."</td>
</tr>";
}
}
use MySQL LEFT
select *,LEFT(description , 50) description from sightseeing

PHP MYSQL if null statement

I need a way to do this, If a mysql query dosn't retrieve any data, something happens. Here's an example.
$color="red";
$query = mysql_query("SELECT * FROM people WHERE shirt='$color'");
if($query = null){
echo 'No people wearing '.$color' shirts available.';
}
Use mysql_num_rows() for this.
if( mysql_num_rows($query) == 0 ) {
// No results returned
Use mysql_num_rows to check, how many rows have been returned by your query.
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
if( $num_rows == 0 ) {
// No results returned
echo "No results!";
} else {
echo "$num_rows Rows\n";
}
?>
Besides the mysql_num_rows() there is also COUNT() which could be used like this:
"SELECT COUNT(*) FROM people WHERE shirt='$color'"
You might actually need more data from the database but if you only need a number then this would remove the need for an if.
You could use empty either:
$num_rows = mysql_num_rows($result);
if(empty($num_rows)){
echo 'No results';
}

Categories