MYSQL SELECT how to know there's something selected or not - php

$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..
}

Related

PHP - Check table to see if entry exists

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

Check MYSQL to see if a entry already exists without if else

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)

mysql value not found

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
}

check if any is 0

How to check with php/sql if any of fields in database that are selected in while loop is 0?
$res = mysql_query('SELECT id, name FROM table'); \\check here?
while($row = mysql_fetch_array)
{
\\or check here?
}
Thanks in advance!
EDIT:
I need to select all fields and then check if any one of them is 0.
$foundZero = false;
$res = mysql_query('SELECT id, name FROM table');
while ($row = mysql_fetch_array($res))
{
if (in_array(0, $row))
{
// This row has a zero
$foundZero = true;
}
}
if ($foundZero)
{
// at least one zero in one row has been found
}
else
{
// No zeros have been found
}
If $foundZero is true, then at least one field in one row is equal to 0. Otherwise, all fields are non-zero.
in the query add a where clause
SELECT id, name FROM table where my_field=0
then you not need to check every results, you get only the wanted results rows.
Obviously, check inside the while loop. For the query may return ZERO row.
$res = mysql_query('SELECT count(*) FROM table WHERE my_field=0'); \\check here?
if(mysql_num_rows($res) > 0)
{
while($row = mysql_fetch_array)
{
\\or check here?
}
}
If you really have to return all rows and then check for zero, add an order by clause to your query to minimize your checking.
SELECT id, name FROM table ORDER BY id DESC
then
if ($id <= 0)
handleIt()
else
proceed()

PHP MYSQL if null statement

I need a way to do this, If a mysql query dosn't retrieve any data, something happens. Here's an example.
$color="red";
$query = mysql_query("SELECT * FROM people WHERE shirt='$color'");
if($query = null){
echo 'No people wearing '.$color' shirts available.';
}
Use mysql_num_rows() for this.
if( mysql_num_rows($query) == 0 ) {
// No results returned
Use mysql_num_rows to check, how many rows have been returned by your query.
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
if( $num_rows == 0 ) {
// No results returned
echo "No results!";
} else {
echo "$num_rows Rows\n";
}
?>
Besides the mysql_num_rows() there is also COUNT() which could be used like this:
"SELECT COUNT(*) FROM people WHERE shirt='$color'"
You might actually need more data from the database but if you only need a number then this would remove the need for an if.
You could use empty either:
$num_rows = mysql_num_rows($result);
if(empty($num_rows)){
echo 'No results';
}

Categories