confuse to make error message in PHP - php

all this code is work, what left is I try to echo a message if query not find anything..
include("config.php");
$search = $_POST['search'];
$sql = mysql_query("SELECT * FROM barangtbl WHERE nama LIKE '%$search%' ") or die(mysql_error());
while ($res=mysql_fetch_array($sql)) {
echo $res['nama'].'<br>';
}
?>

Write this:
if(mysql_num_rows($sql)) {
while ($res=mysql_fetch_array($sql)) {
echo $res['nama'].'<br>';
}
}
else
{
echo "Not found";
}

Try this:
include("config.php");
$search = $_POST['search'];
$sql = mysql_query("SELECT * FROM barangtbl WHERE nama LIKE '%$search%' ") or die(mysql_error());
if(mysql_num_rows($sql))
{
while ($res=mysql_fetch_array($sql)) {
echo $res['nama'].'<br>';
}
}
else
{
echo "The query resulted in an Empty Set";
}
Here,
mysql_num_rows($sql) will output the total no of rows in the result. If the query's output is zero rows, the value of mysql_num_rows($sql) is zero which will lead to the if condition being false and hence executing the else part.

John V is right.
But i would suggest you to use mysqli instead of the deprecated mysql function
(deprecated since version 5.5)
http://php.net/manual/en/book.mysqli.php

try this:
if($sql)
{
echo "success";
}
else
{
echo "Error";
}

Related

why cant i check if my email and mob no already exist or not?

This is my php file in which I am trying to check if the email already exists or not.
<?php
include_once("connection.php");
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$pass=$_REQUEST['pass'];
$mobno=$_REQUEST['mobno'];
$checkemail="SELECT * FROM dhruv_users WHERE email= '$_REQUEST[email]'";
$checkmob="SELECT * FROM dhruv_users WHERE mobno= '$_REQUEST[mobno]'";
$rsemail = mysqli_query($conn,$checkemail);
$rsmob = mysqli_query($conn,$checkno);
$dataemail = mysqli_num_rows($rsemail);
$datamob = mysqli_num_rows($rsmob);
if($dataemail >= 1) {
echo "exists";
}
else if($datamob >= 1)
{
echo "exists";
}
else{
$select=mysqli_query($conn,"select max(id) as id from dhruv_users");
if($data=mysqli_fetch_array($select))
{
$id=$data['id'];
$id++;
}
else
{
$id=1;
}
$query=mysqli_query($conn,"insert into dhruv_users VALUES ('$id','$name','$email','$mobno','$pass')");
if($query)
{
echo "success";
}
else{
echo "unsuces";
}
}
?>
There is no error but data gets entered successfuly without checking mob no if it exists or not.
Entering same mob no again and again shows success message instead of exist message.
Why dont you use mysqli_num_rows instead of mysqli_fetch_array with MYSQLI_NUM .
Try the following
$rs = mysqli_query($conn,$check);
$dataa = mysqli_num_rows($rs);
if($dataa > 1) {
echo "User Already in Exists<br/>";
}
You need to count the result which you getting from DB.
There is a logical error in code. Please have a look on code below:-
Your code
if($dataa[0] > 1) {
echo "User Already in Exists<br/>";
}
Replace above with:
if(count($dataa) > 1) {
echo "User Already in Exists<br/>";
}
You need to write your query with proper quotes. It's unable to recognize the email index of $_REQUEST. Also, use mysqli_num_rows function.
Refer to the code below for best possible practice:
$check = "SELECT * FROM dhruv_user WHERE email= '" . $_REQUEST['email'] . "'";
$rs = mysqli_query($conn,$check);
if ($rs) {
$rowcount = mysqli_num_rows($rs);
if ($rowcount) {
echo "User already exists<br/>";
}
}

get no results for query. its not true,not false and to null

i did some query to check username and password.
when i enter the right data its working ok,
if i put the right email and wrong password its working ok,
when i put a username that do not exist i get no results and white screen. now echo command jump. its looks like its stuck and there is no error.
any idears?
if (isset($email) && isset($password)) {
$query = "SELECT * ";
$query .= "FROM users ";
$query .= "WHERE user_email = '{$email}' ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
if ($row["user_password"] == $password) {
echo json_encode($row);
} else {
echo ('{"user_id":"0","user_name":"","user_email":"","user_password":"","register_date":"2016-03-05","confirm":"0"}');
}
}
} else {
echo("error");
}
} else {
echo($result);
echo("Missing Vars");
}
I bet that "if ($result)" is true but it never enters the while loop since there are no rows to iterate over. This would lead to a blank screen. Try echoing out what is returned from the database and echoing out each nest to see what gets output. Like the following:
if ($result) {
echo("if $result must be true because I made it in");
while ($row = mysqli_fetch_assoc($result)) {
echo("I made it in the while loop if there are rows in my result");
if {
echo("I made it in while's if");
...
} else {
echo("I made it in while's else");
...
}
}
} else {
echo("if $result must be false because I didn't make it in");
echo("error");
}
I bet that using the above example you'll see:
if $result must be true because I made it in
And that is all you'll see

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

php else statement for empty search

I have a search function which matches keywords from a database and echo's out some html but I'm missing how to enable the handling of empty searches. Can I use an else statement or do I have to redefine the same parameters and use !isset for if not set?
<?php
$con = mysqli_connect("localhost", "database", "password", "table");
if (isset($_GET['search'])) {
$search_query = $_GET['search_query'];
global $con;
$get_item = "select * from database where keywords like '%$search_query%'";
$run_item = mysqli_query($con, $get_item);
while ($row_item = mysqli_fetch_array($run_item)) {
$item_keywords = $row_item['item_keywords'];
echo "Search found for $search_query";
} // working fine up to here
} else {
echo "Search not found for $search_query";
}
?>
You can use isset() and mysqli_num_rows() to check empty result. ANd use mysqli_real_escape_string for fire your query
if (isset($_GET['search'])) {
if (isset($_GET['search_query']) && $_GET['search_query'] != "") {/// check variable is set or not
$search_query = $_GET['search_query'];
$$search_query = mysqli_real_escape_string($con, $search_query);//
$get_item = "select * from `database` where `keywords` like '%$search_query%'";
$run_item = mysqli_query($con, $get_item);
$row_cnt = mysqli_num_rows($run_item); // count number of rows
if ($row_cnt > 0) {
while ($row_item = mysqli_fetch_array($run_item)) {
$item_keywords = $row_item['item_keywords'];
echo "Search found for $item_keywords";
}
} else {
echo "Search not found for $item_keywords";
}
} else {
echo "Search not found for $item_keywords";
}
}
Change your condition to:
if (isset($_GET['search']) && trim($_GET['search_query']) != '') {
}
You can set condition for empty search like this.
if (isset($_GET['search']) && trim($_GET['search']) !='') { ... }
Modify your if condition
if (isset($_GET['search']) && !empty($_GET['search_query'])) {
}
You can do it by using the empty function:
if (!empty($_GET['search'])) {
//your code
}

PHP MySQL IF .. ELSE (Else Not Displaying Anything)

I am trying to go through all the records in a table and display something for all the records which has a match on the SQL and display something else for the record that don't have a match.
So I'm doing this:
$result = mysqli_query($con,"SELECT * FROM myTable WHERE id='$my_id' ");
while($row = mysqli_fetch_array($result)) {
if (!empty($row['id'])) {
echo 'Match Exists!';
} else {
echo 'There is NO Match';
}
}
My problem is that there is No echo for the non matches although there is for the matches.
So, I'm getting "Match Exists!" for the matches but no "There is NO Match" for the non matches.
What I'm I doing wrong here?
SELECT * FROM myTable WHERE id='$my_id
here you are checking if there exists any row where the ids matches so your code below loses his property:
if(!empty($row['id'])) {
echo 'Match Exists!';
} else {
echo 'There is NO Match';
}
You are already verifing if exists so there will not be any empty id.
Try this
<?php
$result = mysql_query($con,"SELECT * FROM myTable WHERE id='$my_id' ");
$num=mysql_num_rows($result);
if ($num!=0)
{
echo 'Match Exists!';
} else {
echo 'There is NO Match';
}
?>
OR
<?php
$result = mysql_query($con,"SELECT * FROM myTable WHERE id='$my_id' ");
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$query=$result = mysql_query($con,"SELECT * FROM myTable WHERE id='$id' ");
$num=mysql_num_rows($query);
if ($num!=0)
{
echo 'Match Exists!';
} else {
echo 'There is NO Match';
}
}
?>
If there are no matches, $row will evaluate to FALSE (because no rows are returned). If you have to do it with a while loop, try do { ... } while (...);, like below.
The difference is that the while() condition is checked after the first iteration.
$result = mysqli_query($con,"SELECT * FROM myTable WHERE id='$my_id' ");
do {
$row = mysqli_fetch_array($result);
if (!empty($row['id'])) {
echo 'Match Exists!';
} else {
echo 'There is NO Match';
}
} while(!empty($row['id']));
You have no matches because of your query.
SELECT * FROM myTable WHERE id='$my_id'
The above will fetch only matching records.
Maybe you should alter your query or the line
if (!empty($row['id']))
and check for another field that might be empty
Your query explicitely filters out "non-matching" rows:
WHERE id = '$my_id' -- neither rows having "id != $my_id"
-- nor rows with empty "id" are fetched by this query
You probably want to remove the WHERE condition from your query, and test for if($row['id'] === $my_id) in your PHP loop.
Try to remove "spaces" with trim:
if (!empty(trim($row['id']))) {
echo 'Match Exists!';
}

Categories