If search returns nothing give error - php

I have a search form on my website, and I want it to return an error if the user searches for something that does not exist in my database
<?php
$search = $_POST['search' ];
$numrows = mysql_num_rows ($result );
if($numrows != 0) echo "No results";
?>
This is the code that my search form is using, however the echo"no results" shows on the homepage above all of the results, but does not display when there are no results from the search.
I'm a beginner to PHP, so I am completely unsure why this is happening.
If you need my full code, it is below:
http://pastebin.com/xjxwhfDT

wouldn't it be
if($numrows == 0) echo "No results";
if nothing is found ?
your $numrows = mysql_num_rows ($result ); shouldn't be at the place it actually is.
You should do something like :
$count = count($result);
if($count > 0)
{
while ($row =mysql_fetch_object ($result)) {
// do your stuff
}
}
else
{
echo 'no results';
}
also, what is the point of $search = $_POST['search' ]; in your while loop? You don't seem to use it at all.

Firstly, given the code. I would ask if $result contains a query result?
ie.
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
Secondly, mysql_num_rows() returns number of rows or FALSE. So, double check if $results is a valid result set from a valid query.
Hope this helps.

move your if before the while and it works
Now
while ($row =mysql_fetch_object ($result)) {
[...]
$numrows = mysql_num_rows ($result );
if($numrows != 0)
[...]
Correct
$numrows = mysql_num_rows ($result );
if($numrows == 0){ echo 'no result'; die();}
while ($row =mysql_fetch_object ($result))
[...]
Because if result is empty while loop is not executed.

You can use:-
$numrows = mysql_num_rows ($result );
if($numrows > 0){
while ($row =mysql_fetch_object ($result)) {
//- Main Content -
echo '<div class="wrapper">';
?>
<?php
//--------- GET THE POST HTML VARIABLES. PUT INTO PHP VARIABLES -----------------------------
$search = $_POST['search' ];
}
// write your whlie loop code here
}else{
echo "No results";
}
mysql_num_rows Retrieves the number of rows from a result set. Check number of row greater then 0 or not

Related

Script Calls Wrong echo

I am trying to return data from my database. The query will return a zero or one. If the data returns one (if psv1=1) I want to echo 'Data found'. If the data returns a zero (if psv1=0) I want to echo 'No data found'.
When I run my script I always get 'No data found', even when psv1=1. I also tried to change $res[0] == 1 to $res[0] > 0 but it didn't work.
$result = mysqli_query($db_handle, 'SELECT psv1 FROM cus WHERE id="1" AND user_id="'. $_SESSION['user_id'] .'"');
$res = mysqli_fetch_row($result);
if ($res[0] == 1){
echo 'Data found';
}
else
{
echo "No data found";
}
You should use mysqli_fetch_array($result, MYSQLI_ASSOC):
$result = mysqli_query($db_handle, 'SELECT psv1 FROM cus WHERE id="1" AND user_id="'. $_SESSION['user_id'] .'"');
$res = mysqli_fetch_array($result, MYSQLI_ASSOC);
if ($res['psv1'] == 1){
echo 'Data found';
}
else {
echo "No data found";
}
I wonder if you need both conditions in the WHERE. Are you meaning to have id and user_id. Suggest removing:
id="1" AND
Can only be a guess, but seems unlikely condition.
On checking no results returned, may I suggest trying:
$result = mysqli_query($db_handle, 'SELECT psv1 FROM cus WHERE id="1"
AND user_id="'. $_SESSION['user_id'] .'"');
$res = mysqli_fetch_row($result);
if(mysqli_num_rows($result) > 0){
echo 'Data found';
}
else {
echo "No data found";
}
See related SO answer on mysqli_num_rows
According to the Manual, mysqli_fetch_row() will return NULL on failure or else an array of strings corresponding to a row of data as an enumerated array. I suggest also testing that mysqli_query() does not return FALSE as per the Manual's recommendation by way of numerous examples here. So, a better test would be to code something like the following with $query having been set to the specific query:
<?php
$result = null;
$res = null;
$result = mysqli_query( $db_handle, $query ) OR die( mysqli_error( $db_handle ));
if( ( $res = mysqli_fetch_row( $result )) === NULL ) {
// Nothing to fetch
}
else
{
echo ($res[0] == 1 )? "Data found." : "No data found";
}

Using Mysqlii_query result object to check if the return value match the desired result

So I want to search the database and return a result, take a look at this query:
$sql = "SELECT workforce from users WHERE email='$username'";
$result = mysqli_query($con,$sql);
if($result == "General"){
echo "General Category";
}else
echo "Manager Category"
I found out that the result returned is not a string but an object, thus I am unable to find a way to achieve the desired result.
For this example I am expecting a single result. Also I want to return the result to a android code via Volley Library which I have already done.
I am stuck with the php part since I am not familiar with it.
You can try this:
Read for more info : mysqli_query mysqli_fetch_assoc
$sql = "SELECT workforce from users WHERE email='$username'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if ($row['category'] == 'general') {
echo 'general category';
}else
{
echo 'manager category';
}
}
} else {
echo "0 results";
}
here you go
<?
$sql = "SELECT workforce from users WHERE email='$username'";
if($result = mysqli_query($con,$sql)): //check query
$row = mysqli_fetch_assoc($result);
if($row['workforce'] == "General") {
echo "General Category";
} else {
echo "Manager Category"
}
endif;

PHP $result = mysql_query($query) returns true even if there is no value in database

I have the following code.
$query = "SELECT HealthStatus FROM healthstatus where HealthStatus=$HealthStatus";
$result = mysql_query($query);
echo $HealthStatus;
if($result = false)
{
//do something
}
else
{
//print value already exists
}
I don't get any error or warning when the code is executed. But, even if $HealthStatus exists in database, the if part gets executed. When I give echo $HealthStatus, the value fetched is printed correctly.
I have tried using if(!$result). That doesn't work either. Can someone help me.
You have to use mysql_num_rows to know if the query returned any rows, eg:-
if($result && mysql_num_rows($result))
{
// a row exists
}
else
{
// do something
}
also if HealthStatus is a string it needs to be enclosed in quotes eg:-
$query = "SELECT HealthStatus FROM healthstatus where HealthStatus='".$HealthStatus."'";
$result = mysql_query($query);
if($result && mysql_num_rows($result))
{
// a row exists
$row=mysql_fetch_array($result);
echo "Health status was ".$row["HealthStatus"];
}
else
{
// do something
echo "There were no rows found";
}
To understand how much rows were received use mysql_num_rows function.
if(mysql_num_rows($result) > 0) {
} else {
}
Also, you have error in your if:
if($result = false)
{
//do something
}
else
{
//print value already exists
}
You assign false to $result in your if statement.
You have to use if($result == false).
To avoid such mistakes you can change order:
if(false == $result)
This will work, but this:
if(false = $result)
Will cause error.
Hope, this will help.

PHP MYSQL Get data from database and echo message if no data retrieved

Im trying to make a code where it displays all names in database where "clanwars" is set to 1 (int) and if all of them is 0 echo an message like: "Noone has signed up yet!"
This is some of the code i have:
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
while($row = mysqli_fetch_array($result))
{
if ($row['clanwars'] != '0') {
echo $row['mantra']."<br>";
} else {
echo 'Noone has signed up yet!';
}
}
mysqli_close($con);
First, in your example row['clanwars'] will never equal to 0 because you already specified WHERE clanwars = 1 in your query, so MySQL will return only those that have clanwars=1. If I understand well, you need to do something like:
<?
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
if (mysqli_num_rows($result)==0) echo 'Noone has signed up yet';
else {
while ($row = mysqli_fetch_array($result)) {
//do what you need
}
}
?>
So basically, you retrieve everyone who has CLANWARS set to 1 in the database. If there are records, process them, if there are no records, it means that nobody has signed up.
Is this what you need?
Try this:
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
if (mysqli_num_rows($result) == 0)
{
echo 'Noone has signed up yet!';
}
else
{
while ($row = mysqli_fetch_array($result))
{
echo $row['mantra']."<br>";
}
}
mysqli_close($con);

PHP MYQSLi Returning a different value if field is empty

This should be simple.... but it's taking a while... Here's the code that's not working (it either shows nothing or the blank state message each time). $show image is the query and I know it's running fine.
// BLANK STATE TOGGLE
$result = mysqli_fetch_array($showimage, MYSQLI_ASSOC);
if($result == ''){
echo '<p>Sorry- no image.</p>';
}
else {
echo '<p>There is an image!</p>';
}
}
If you only want to check for the existence of rows in the result from your query, why don't you simplify it like this
// $db is your MySQLi connection object
$query = 'SELECT COUNT(1) FROM `table` WHERE `something` = ?';
$stmt = $db->prepare($query);
$stmt->bind_param('s', $something);
$stmt->execute();
$stmt->bind_result($rowCount);
$stmt->fetch();
$stmt->close();
if ($rowCount > 0) : ?>
<p>There is an image!</p>
<?php else : ?>
<p>Sorry- no image.</p>
<?php endif ?>
mysqli_fetch_array returns null if there is no match in the database. So you need to check for null.
You may need to try this:
if $showimage is your query ..
//This should run fine
//$link is ur connection
$new_result = mysqli_query($link,$showimage);
$result = mysqli_fetch_array($new_result, MYSQLI_ASSOC);
if($result == null){
echo '<p>Sorry- no image.</p>';
}
else {
echo '<p>There is an image!</p>';
}
}

Categories