While loop / Null Value

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
slomotion
Forum Newbie
Posts: 13
Joined: Fri Jun 22, 2007 9:15 pm

While loop / Null Value

Post by slomotion »

I am looking to list the column names that have values that are not null in a certain row.
What I have so far is the fallowing

Code: Select all

<?php
error_reporting(0);

function mysqls_sad()
{
        echo "MySQL said: Error: " . mysql_errno() . ' - ' . mysql_error();
    exit();
}

if (!is_resource(($dbcon = mysql_connect("db949.perfora.net", "dbo207711380", "###pass##"))))
{
    mysqls_sad();
}

if (!mysql_select_db('db207711380', $dbcon))
{
    mysqls_sad();         
}
$flavor = $_GET["flavor"];
$sql = 'SELECT * FROM `flavors` WHERE `flavor`=\'Apple\' LIMIT 0, 30 ';
if (false === ($result = mysql_query($sql)))
{
        mysqls_sad();
}
while($brand = mysql_fetch_field($result))
      {
  if ($brand->name != 'flavor_id' && $brand->name != 'flavor' && $brand->name != 'desc' && $brand->not_null!='1')
            { 
            echo $brand->name;
            echo "<br/>"; 
            }
      }            
?>
what I have it doing so far is displaying the columns name, while taking out those that I dont want to display(like id and such). But then I dont know how to check to see if that value is/isnt Null.

Do you guys have any idea or am I on the completely wrong track.
Thanks you guys have been so helpful with this stuff.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

To test is something is not null you can use..

Code: Select all

// ensure it is anything BUT null
$x !== null

// check if it is an empty string, null, 0 or boolean false
empty($x)
slomotion
Forum Newbie
Posts: 13
Joined: Fri Jun 22, 2007 9:15 pm

Post by slomotion »

ok so I thought I was doing it right but got an error message
a revamp to my coding

Code: Select all

<?php
error_reporting(0);

function mysqls_sad()
{
        echo "MySQL said: Error: " . mysql_errno() . ' - ' . mysql_error();
    exit();
}

if (!is_resource(($dbcon = mysql_connect("db949.perfora.net", "dbo207711380", "x6cFkX.R"))))
{
    mysqls_sad();
}

if (!mysql_select_db('db207711380', $dbcon))
{
    mysqls_sad();         
}
$flavor = $_GET["flavor"];
$sql = 'SELECT * FROM `flavors` WHERE `flavor`= `Apple`';
if (false === ($result = mysql_query($sql)))
{
        mysqls_sad();
}
while($newArray = mysql_fetch_assoc($result))
      {
      if (empty($newArray))
      {
                  //nothing
      }
      else
      {
            $brand = mysql_fetch_field($newArray);      
            echo $brand->name;
            echo "<br/>";
      }
      }           
?>


This same error message I thought I had it right but maybe not. a 1054 message.
but I dont know what is wrong with it.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

What field are you checking?

You can add it to the query like you asked the other day. But if you want to do it in PHP you have to specify the array index.

Code: Select all

if (empty($newArray['FIELD_NAME_HERE']))
      {
                  //nothing
      }
slomotion
Forum Newbie
Posts: 13
Joined: Fri Jun 22, 2007 9:15 pm

Post by slomotion »

Dang Im still getting used to this array thing.
Im trying to check each one that comes through (if that makes sense), or all of them except the ones that I have set out.
I think I need to take a class for this stuff, I need to learn more of the rules.
Post Reply