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";
}
Related
I have this php code:
$query = $database->query("SELECT EXISTS(SELECT * FROM contacts WHERE contact_id = '$contactID')";
if($query == 0){
echo "not registered";
}elseif($query == 1){
echo "registered"
}
If I'm not wrong, the query is suppose to return 0 or 1 and it works in my SQLite manager. What is the correct way on getting that value in Php and use it in IF ELSE statement?
If you only need a single value, you can use querySingle:
$result = $database->querySingle("SELECT EXISTS(SELECT * FROM contacts WHERE contact_id = '$contactID'");
Otherwise, with normal queries, the result returned by ->query isn't actually the data itself, but an identifier you would use to get data from the database:
$results = $db->query('SELECT bar FROM foo');
while ($row = $results->fetchArray()) {
var_dump($row);
}
I need to check if a record exists in a table before adding it.
I've done some digging and this is what people keep coming back too:
$result= mysql_query("SELECT id FROM mytable WHERE city = 'c7'");
if(mysql_num_rows($result) == 0) {
// row not found, do stuff...
} else {
//row found, do other stuff...
}
or some variation there of.
This logic is exactly what I need except for the fact that $result is never returning a positive result.
The record does exist and should return a positive result.
I also tried
$sql="SELECT COUNT(email) FROM table WHERE email=$mail;";
$yesorno = mysqli_query($sql);
echo $yesorno ;
as a test and the echo returns no value.
you need to check first if the query succeed running maybe there is a problem with it
$Query = "Select id from mytable where city = 'c7' ";
if($result = mysql_query($Query)) {
if ( mysql_num_rows($result) == 0 ) {
// no rows found ;
} else {
// row exist ;
}
} else {
echo "Query failed ". $Query;
}
$result1 = mysql_query("SELECT id FROM p_u_r WHERE p_name = '$d'");
$S=$row['id'];
if(mysql_num_rows($result1) == 0) {
row not found, do stuff...
}
this code send an error of undefined index error. I have created p_u_r table in my database but no data is inserted.
$result1 = mysql_query("SELECT id FROM p_u_r WHERE p_name = '$d'");
$S=$row['id'];
if(mysql_num_rows($result1) == 0) {
row not found, do stuff...
}
It is likely because $row... doesn't exist. You should write something like that:
// assuming you have created $d already...
$result1 = mysql_query("SELECT id FROM p_u_r WHERE p_name = '$d'");
if(mysql_num_rows($result1) == 0) {
// row not found, do stuff...
}
else {
//fetch your results using mysql_fetch_assoc() or mysql_fetch_row()
//...
}
BTW Do you know using MySQL extension is DEPRECATED and you should create code using MySQLi ?
I've found several tutorials which have similar code like the following:
$sql = "select * from users";
$result = $conn1->Execute($sql);
if ($result==false) {
print 'error' . $conn1->ErrorMsg() . '<br>';
} else {
print_r($result->GetRows());
}
But how can $result ever be false? If I add a where clause which can not be fulfilled the else-branch is still taken since $result contains the column titles. Examples:
"select * from users"; // Select the whole table data
echo "$result";
leads to
id,username,password 1,peter,geheim 2,sabine,secret 3,thorsten,qwertz
Whereas
"select * from users where username = 'does not exist'";
echo "$result";
leads to
id,username,password
Therefore result is never false. What is my mistake here?
The Execute method returns false if the query itself fails, and not if it has 0 results.
If you want to check if the query returned any results you can use the RecordCount method.
$rows = $conn1->Execute($sql);
if ($rows->RecordCount() > 0) {
// Do something with your rows
} else {
// Nothing returned
}
I am trying to check if a name exists. Query should return 0 because the field is empty but returning 1 instead. help.
function profile_exists($user) { //checks that profile exists
$profile_ex = mysql_query(
"SELECT COUNT(profile_name) FROM user_info WHERE id = '{$user}'");
if(!confirm_query($profile_ex)) {
return false;
} else {
if (mysql_result($profile_ex, 0) == 1) {
echo mysql_result($profile_ex, 0);
exit;
return true;
} else {
return false;
}
}
}
You have one row instead of 0 rows because COUNT(*) returns one row of data, the count which has the value of 0 in this case.
You will need to run the query and then check the value of count.
Example:
$result = mysql_query("SELECT COUNT(*) AS count FROM ...");
$row = mysql_fetch_assoc($result);
if( $row['count'] == 0){
return False;
}else{
return True;
}
Your syntax looks abit complex, try this instead:
$profile_ex = mysql_query("SELECT * FROM user_info WHERE id = '$user'");
$profile_ex = mysql_num_rows($profile_ex);
if ( $profile_ex > 0 ) {
echo "exists"; } else {
echo "doesn't exist";
}
Hope this helps
You should be using mysqli_ or PDO with prepared statements to prevent against SQL injection.
It will always return 1 because you are using an aggregate function (COUNT). Select a column from your table instead:
SELECT profile_name FROM ...
However, if you are only interested in checking whether the profile exists, use mysqli_num_rows. If you wish to check whether the profile exists and return data, use mysqli_fetch_assoc.