I have a mysql table and am using php. I have:
mysql_query("SELECT id FROM table WHERE email='$email' LIMIT 1");
Now this returns an ID if email was found. However, I want to do something if it does not find $email in the email table column. How can I recognise when $email was not found and tell php?
mysql_query returns a result. You can call mysql_num_rows on that result to see the number of rows selected:
$result = mysql_query("SELECT id FROM table WHERE email='$email' LIMIT 1");
if (mysql_num_rows($result) > 0) {
// found a result
} else {
// no result found
}
I think that mysql_num_rows check should help.
If mysql_num_rows() returns 0, no row was selected because no email matched. http://docs.php.net/mysql_num_rows
All that is required to check if a result was not found is an if statement used with the mysql_num_rows function:
$check = mysql_query("SELECT id FROM table WHERE email='$email' LIMIT 1");
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
// No email found, so show an error to the user
echo "No email found!";
}
else
{
// An email address was found, so do something with it here
}
Related
I have written code in php in order to get id from url and check whether it exists or not in mysql database. I have an existing value in the database but still getting a problem saying value is not found. Please anyone have a solution?
Here is my code:
<?php
require 'Common.php';
$Email=$_GET['id'];
$result = mysql_query("SELECT email FROM cdcol.employees WHERE email='$Email'");
if(mysql_num_rows($result) >0)
{
echo 'Email Found';
}
else
{
echo 'Email NOT Found';
}
?>
Typo:
$result = mysql_query("SELECT email FROM cdcol.employees WHERE email='"mysql_real_escape_string($Email)"');
=>
$result = mysql_query("SELECT email FROM cdcol.employees WHERE email='".mysql_real_escape_string($Email)."'");
To improve the performance of this query, use LIMIT 1 (you check if there is more than 0 lines):
$result = mysql_query("SELECT email FROM cdcol.employees WHERE email='".mysql_real_escape_string($Email)."' LIMIT 1");
Hi I have a long page of code with multiple if else statemets, what i would like to do is check a database to see if that Ip address is already in the table
currently i am doing it like this
$result = mysql_query("SELECT * FROM masterip_details WHERE ip_address='$ip_address' AND client_id='$client_id'") or die(mysql_error());
$num_rows = mysql_num_rows($result);
//IF THE RESULT IS MORE THAN 0, THIS MEANS THAT THEY ARE A RETURNING VISITOR
if( $num_rows > 0 ) {
/// Add returning Script here
} else {
//Add code
}
Is there a way i could do this with out the if else statement?, so for example if the record was in the database just return a value of 1.
Thanks, any suggestions would be appreciated.
You can use count to get the number of rows with that value.
$result = mysql_query("SELECT count(*) FROM masterip_details WHERE ip_address='$ip_address' AND client_id='$client_id'") or die(mysql_error());
return $result > 0; //returns a boolean
This way you will get a 0 if the value doesn't exist on the database and a number higher than 0 if it does.
Make use of SELECT IF EXISTS on MySQL.
SELECT IF( EXISTS(
SELECT *
FROM masterip_details
WHERE `ip_address`=? AND `client_id`=?), 1, 0)
$result=mysql_query("SELECT * FROM $name WHERE date='$date'");
How to check whether anything selected or not.
i have tried these but of no use :
if($result == NULL ){} , if(!$result){}
Help !
You can use
mysql_num_rows ($result);
to get the number of rows returned
if(!$result)
Will only tell wether the query executed or not
You can count number of records by
$num_rows = mysql_num_rows($result);
if($num_rows > 0)
{
//do something..
}
I have this code:
$local_id = $_GET['id'];
$sql = dbquery("SELECT * FROM `videos` WHERE `id` = ".$local_id." LIMIT 0, 1");
while($row = mysql_fetch_array($sql)){
$video_id = $row["youtube_id"];
// the rest
}
how can i check if $local_id does not exist in the db and display an error?
mysql_num_rows
if(mysql_num_rows($sql) == 0) {
//Show error
}
$sql = dbquery("select count(*) from videos where id = ".$local_id." LIMIT 0, 1");
$row = mysql_fetch_row($sql);
if($row[0] == 0)
echo 'error';
You can use the following query:
"SELECT COUNT(*) FROM `videos` WHERE `id` = ".mysql_real_escape_string($local_id)
This query will return one number: how many records have matched your query. If this is zero, you surely know that there are no records with this ID.
This is more optimal than other solutions posted in case you only want to check for the existence of the ID, and don't need the data (if you use SELECT * ..., all the data will be unnecessarily sent from MySQL to you). Otherwise mysql_num_rows() is the best choice, as #Ryan Doherty correctly posted.
Be sure to ALWAYS escape data that came from the outside (this time GET) before you put it into a query (mysql_real_escape_string() for MySQL).
If you fail to do so, you are a possible victim for SQL Injection.
You could have a $count variable and increment it in the while loop. After the loop, check the count, if it is 0, then echo an error message.
How could i get $result too equal nothing if the column doesn't exist in PHP?
I was thinking something like this:
$result=mysql_query("SELECT * FROM users WHERE username=$key DESC LIMIT 1")or die ($result = '');
But i was told thats wrong.
It's wrong because you're killing the script with die when a DB error occurs, rather than doing stuff when you find no row.
What you presumably need is more like:
$result = mysql_query($query);
if ($result) {
if ($row = mysql_fetch_assoc($result)) {
// do stuff with row
} else {
// do stuff without row
}
} else { // not needed but left here for illustration purposes
// this is the part that would occur, had you called mysql_query(...) or die;
die(mysql_error());
}
$result=mysql_query("SELECT * FROM users WHERE username= '$key' LIMIT 1")or die (mysql_error());
then check the result of mysql_num_rows()
If you mean that the result returns 0 rows, you can check with mysql_num_rows, like this:
$result=mysql_query("SELECT * FROM users WHERE username=$key DESC LIMIT 1");
if (mysql_num_rows($result) == 0)
$result = '';
Your code will set $result to '' if there's an error, in which case mysql_query returns false. It will also halt the code, since you're calling die(). An empty result set is not an error, however. In that case mysql_query returns a valid resource identifier with no rows. If I understand your question, this is what you want to do:
$result=mysql_query("SELECT * FROM users WHERE username=$key DESC LIMIT 1");
if (mysql_num_rows($result) == 0){
$result = '';
}
<?php
// Here I assume you're using PHP PDO
$pdo = new PDO("mysql:server=localhost;dbname=mydatabase", "root", "");
$result = $pdo->query("SELECT * FROM users WHERE username=$key DESC LIMIT 1");
$errorcode = $pdo->errorCode();
$errorinfo = $pdo->errorInfo();
// Columns doesn't exist
if($errorcode == "43072") $result = "";
// Other error...
else if($errorcode != "00000") die("MySQL Error: " . $errorinfo[2]);
?>