Jump to content

Selecting empty columns


aeris130

Recommended Posts

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.

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

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?
Link to comment
https://forums.phpfreaks.com/topic/4231-selecting-empty-columns/
Share on other sites



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) [snapback]352137[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Try something like:

[code]

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

[/code]
[/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:
[code]
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>";

[/code]

How do I check a single column on a single row to see if its empty or not?
Link to comment
https://forums.phpfreaks.com/topic/4231-selecting-empty-columns/#findComment-14696
Share on other sites

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:
[code]<?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";
}?>[/code]

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

Ken
Link to comment
https://forums.phpfreaks.com/topic/4231-selecting-empty-columns/#findComment-14706
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.