Jump to content

When query is empty


Erwin007
Go to solution Solved by Barand,

Recommended Posts

I have this code and it always gives me "ticket exists" even when it doesn't, where I go wrong?
Thanks.

$query = "SELECT * FROM sales WHERE sales_ticketnr = '$ticket' ";
$result = mysqli_query($con,$query);
										
if(!empty($result))
	{
		echo '<script>alert("ticket exists")</script>';
	}
else
	{
		echo '<script>alert("ticket does NOT exist")</script>';
	}

 

Link to comment
Share on other sites

$result is a mysqli_result object. it will only be an empty() (false) value when the query fails due to an error. a query that matches zero rows is a successful query, not an error.

if all you are doing is testing if a value exists in the database table, without wanting to fetch and use the matching data, use a SELECT COUNT(*) ... query, then fetch and test the count value. if you are going to use the fetched data, just fetch and test the value returned by the fetch statement. if this is for deciding if you are going to insert new data or update existing data, the column in question in the database table should be defined as a unique index, then you would just attempt to insert/update the row of data and test if a duplicate index error (number) occurred.

next, don't put external, unknown, dynamic values directly into an sql query statement, where any sql special character in a value can break the sql query syntax, which is how sql injection is accomplished. use a prepared query instead.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.