Page 1 of 1

While loop / Null Value

Posted: Sat Jun 23, 2007 10:39 pm
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.

Posted: Sat Jun 23, 2007 10:44 pm
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)

Posted: Sat Jun 23, 2007 11:13 pm
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.

Posted: Sat Jun 23, 2007 11:17 pm
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
      }

Posted: Sat Jun 23, 2007 11:27 pm
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.