Script Calls Wrong echo - php

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";
}

Related

How do I get desired value if no entry is found in the database

I have 2 columns in a table. 'phrase' and 'count'.
if($conn->connect_error){
echo 'Connection Faild: '.$conn->connect_error;
}
else{
$just_str = 'ghsfghffgh';
$sql="select * from dbtest where phrase like '%$just_str%'";
$res=$conn->query($sql);
while($row=$res->fetch_assoc()){
$jso = $row["count"];
}
}
exit(json_encode(array("name"=>$jso)));
I want it to return "Not found" as JSON response if there is no such entry in the database.
Note: $just_str variable is user input.
Since you only get one value, you don't need a loop. So just check whether any row is returned.
$row = $res->fetch_assoc();
if ($row) {
$jso = $row['count'];
} else {
$jso = 'Not 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;

check if MySQL table is empty

I want to check if my table is empty I've tried this "which I think that is the solution"
$test_empty="SELECT *FROM objectif where 1 ";
if(empty($test_empty))
{
echo "I m here";
}
But it seems that it doesn't work.
Depending on how you are connecting to your database (for example, using mysqli):
$db = new mysqli("localhost","username","password","dbname");
$check = $db->query("SELECT COUNT(*) FROM objectif");
if ($check->num_rows == 0 || $check->fetch_field() == 0){
echo "table is empty";
}else{
echo "table is not empty";
}
Currently, your code isn't actually connecting to the database or querying the table - you are essentially just checking if the variable $query is empty (which it never will be, as it contains a string!
Running a query to fetch the number of records and checking that as per the code above is one way to do this.
Use this
$mysqli = new mysqli("localhost","root","","db");
if ($result = $mysqli->query("SELECT * FROM `table` LIMIT 1"))
{
if ($obj = $result->fetch_object())
{
echo "NOT EMPTY";
}
else
{
echo "empty";
}
$result->close();
}
$mysqli->close();
Please try below code :
$test_empty="SELECT * FROM objectif";
$query = mysql_query($test_empty);
if(mysql_affected_rows() > 0)
{
echo "It is Empty";
}

If search returns nothing give error

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

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