Jump to content


Photo

Selecting empty columns


  • Please log in to reply
4 replies to this topic

#1 aeris130

aeris130
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 06 March 2006 - 06:05 PM

Suppose I assign a value to $my_line by selecting all data from a certain column in my MySQL table.

I'm trying to get a certain output in case this column doesn't contain any data, but I don't know what syntax to use.

if ($my_line=="no_data_in_this_column")
echo "no data"; 
else
echo "Current data is: $my_line";

What should I replace "no_data_in_this_column" with?

Also, what is the syntax to simply not do anything at all in case $my_line is empty?

#2 mem0ri

mem0ri
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 06 March 2006 - 06:16 PM


Try something like:


$query="SELECT * FROM table";
$qresult = mysql_query($query);

if(mysql_num_rows <= 0)
{
--Code to Use if no results--
}
else
{
--Code to Use if results--
}



#3 aeris130

aeris130
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 06 March 2006 - 06:30 PM



Edit: Solved



[!--quoteo(post=352137:date=Mar 6 2006, 01:16 PM:name=mem0ri)--][div class=\'quotetop\']QUOTE(mem0ri @ Mar 6 2006, 01:16 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Try something like:


$query="SELECT * FROM table";
$qresult = mysql_query($query);

if(mysql_num_rows <= 0)
{
--Code to Use if no results--
}
else
{
--Code to Use if results--
}

[/quote]

Can that be used on mulitple columns as well? Suppose I have a row with the following columns:

My_table
- ID
- Name
- Phone

And I want to display the following on screen:
if (ID == empty)
echo "no IDS"; 
else
echo "ID is: $ID <br>"; 


if (phone == empty)
echo "This person doesn't have a phone"; 
else
echo "$phone<p>"; 


How do I check a single column on a single row to see if its empty or not?

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 March 2006 - 06:46 PM

The first responder's suggestion only checks if there are no rows returned from a particular query, it will not check if individual fields are null or blank.

To do that, try:
<?php
$q="SELECT * FROM table";
$rs = mysql_query($query);
while ($rw = mysql_fetch_asoc($rs)) {
   $tmp = array();
   foreach($rw as $k => $v) switch ($k) {
        case 'ID':
              if ($v == '') $tmp[] = 'No ID entered'
              else $tmp[] = 'ID is ' . $v;
              break;
        case 'Phone':
              if ($v == '') $tmp[] = "This person doesn't have a phone";
              else $tmp[] = 'Phone: ' . $v;
              break;
         }
    echo 'For ' . $rw['name'] . "<br>\n" . implode("<br>\n",$tmp). "<br>\n";
}?>

Note: I haven't checked the above for syntax errors.

Ken

#5 krixham

krixham
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 March 2006 - 08:02 PM

Ken - THANK YOU. This is very similar to what I was trying to do with one of my scripts, but couldn't figure out where to put it. This response put me in the right area.

Kathy




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users