query if row in mysql empty, otherwise...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

query if row in mysql empty, otherwise...

Post by gwolff2005 »

Hi guys I need your help.
I need a code saying: If row "x", "y", "z" of table "user" in database" "guntmar" is empty display the following text "is empty" otherwise display the content as a graph.. How can i do that?
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: query if row in mysql empty, otherwise...

Post by it2051229 »

ok i don't really get what you're saying there.. but I understood the "query if row in mysql is empty"..

let me give you an example, but i'm not going to make use of x y or z.
let's say I want to query all persons in the database with the name "john"...

so i do something like

Code: Select all

$johnQuery = mysql_query("SELECT name FROM persons WHERE name='JOHN' ") or die(mysql_error());
so the question now is, how to check if there is a result?
you do something like

Code: Select all

 
if(mysql_num_rows($johnQuery) > 0)
{
     echo "There are ".mysql_num_rows($johnQuery)." john(s) queried.";
}
else
{
    echo "There are no john queried.";
}
 
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: query if row in mysql empty, otherwise...

Post by tech603 »

What your looking for is

Code: Select all

 
/* query result */
$result = mysql_query("your query here ");
 
if (!empty($result)){
/* list your rows */
} else {
echo "No results to be displayed";
}
 
Hope that helps
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: query if row in mysql empty, otherwise...

Post by gwolff2005 »

hi guys,
maybe something really simple, but i dont get it.
I declare variables at the beginning of the session. Later I want to show a graph to the user (if the data is in mysql), if they are not stored in mysql a text should come up, saying: "You need to do the test." Still it does not work.
My session variable is

Code: Select all

$_SESSION['MM_resultsmo'] = $row['resultsmo'];
and my code is:

Code: Select all

if (!empty(MM_resultsmo)){
echo "You need to do the test";
} else {
echo " " . $_SESSION['MM_resultsmo'];
}
What is wrong???
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: query if row in mysql empty, otherwise...

Post by pickle »

It sounds like you're not checking if the row is empty, but rather certain fields in a row.

You can check with strlen() I think. The field will always be set, so empty() won't work (I don't think).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply