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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.