Page 1 of 1
while loop / array problems
Posted: Fri Jun 22, 2007 9:20 pm
by slomotion
yeah Im new to php and am trying to load a list of flavors when the brand isnt null. my current coding looks like this:
Code: Select all
<?php
//open connection
$conn = mysql_connect("db949.perfora.net", "dbo207711380", "######");
// pick the database to use
mysql_select_db('db207711380',$conn);
//create sql statement
$sql="SELECT flavor from flavors WHERE Al_Ajamy IS NOT NULL ORDER BY flavor";
$result=mysql_query($sql,$conn) or die(mysql_error());
while($newArray=mysql_fetch_array($result)){
$flavor=$newarray['flavor'];
echo "$flavor <br/>";
}
?>
it comes up blank I dont get why it is doing it. I keep editing things and cant figure out what Ive done wrong.
Thanks for the help ahead of time.
EDIT: I changed to IS NOT NULL and it still is blank
Posted: Fri Jun 22, 2007 9:26 pm
by Benjamin
Post the entire code. Mask your password.
Posted: Fri Jun 22, 2007 9:27 pm
by bdlang
Welcome to the forum.
That query will always return NULL. Any comparison or calculation with a NULL returns NULL. You have to use the keywords IS NULL or IS NOT NULL.
Please post your PHP within the appropriate [ PHP ] tags.
Posted: Fri Jun 22, 2007 9:59 pm
by slomotion
I think its in my while loop because when i plug in the SELECT code in phpmyadmin it brings up the proper list. I dont know what it could be but just thought I would try to help you help me

Posted: Fri Jun 22, 2007 10:01 pm
by superdezign
Odd. How many times does it loop?
Posted: Fri Jun 22, 2007 10:03 pm
by slomotion
I dont know how many times it loops. is there a way to figure it out? //beginner here
when it loads, it doesnt really it just stays blank and doesnt have any error message.
its suppost to loop like 30 times, is the amount of data that should be displayed.
Posted: Fri Jun 22, 2007 10:04 pm
by Benjamin
Dude Wrote wrote:Post the entire code. Mask your password.
Posted: Fri Jun 22, 2007 10:07 pm
by slomotion
Code: Select all
<?php
//open connection
$conn = mysql_connect("db949.perfora.net", "dbo207711380", "###PASS###");
// pick the database to use
mysql_select_db('db207711380',$conn);
//create sql statement
$sql="SELECT flavor from flavors WHERE Al_Ajamy IS NOT NULL ORDER BY flavor";
$result=mysql_query($sql,$conn) or die(mysql_error());
while($newArray=mysql_fetch_array($result)){
$flavor=$newarray['flavor'];
echo "$flavor <br>";
}
?>
that the code, I also edited the original post
Posted: Fri Jun 22, 2007 10:14 pm
by Benjamin
Your using
mysql_fetch_array() when you want to be using
mysql_fetch_assoc().
This will work..
Code: Select all
<?php
error_reporting(0);
function mysqls_mad()
{
echo "MySQL said: Error: " . mysql_errno() . ' - ' . mysql_error();
exit();
}
if (!is_resource(($dbcon = mysql_connect("db949.perfora.net", "dbo207711380", "###PASS###"))))
{
mysqls_mad();
}
if (!mysql_select_db('db207711380', $dbcon))
{
mysqls_mad();
}
if (false === ($result = mysql_query("SELECT `flavor` FROM `flavors` WHERE `Al_Ajamy` IS NOT NULL ORDER BY `flavor`")))
{
mysqls_mad();
}
echo "QUERY RETURNED: " . number_format(mysql_num_rows()) . " ROWS\n";
while ($x = mysql_fetch_assoc($result))
{
echo $x['flavor'] . "<br />";
}
Posted: Fri Jun 22, 2007 10:18 pm
by slomotion
Wow I have alot to learn. Thanks to everyone, and Im sure Ill get to know you better this summer!
Yes it is working thanks for your expertise.
Posted: Fri Jun 22, 2007 10:37 pm
by superdezign
If you look at the mysql_fetch_array() documentation, you'll see that it does, in fact, return an associative array as well as a numerical.
Posted: Fri Jun 22, 2007 11:25 pm
by bdlang
superdezign wrote:
If you look at the mysql_fetch_array() documentation, you'll see that it does, in fact, return an associative array as well as a numerical.
I think that's what
astions meant.
BTW, the ORDER BY is unnecessary, your SELECT only names one column, `flavor`, there is no reason to ORDER BY `flavor` unless you want to ORDER BY DESC (descending).