while loop / array problems

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 / array problems

Post 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
Last edited by slomotion on Fri Jun 22, 2007 9:39 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Post the entire code. Mask your password.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

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

Post 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 :D
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Odd. How many times does it loop?
slomotion
Forum Newbie
Posts: 13
Joined: Fri Jun 22, 2007 9:15 pm

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Dude Wrote wrote:Post the entire code. Mask your password.
slomotion
Forum Newbie
Posts: 13
Joined: Fri Jun 22, 2007 9:15 pm

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

astions wrote:Your using mysql_fetch_array() when you want to be using mysql_fetch_assoc().
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.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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).
Post Reply