Basic

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
adi5402
Forum Newbie
Posts: 10
Joined: Mon Jun 23, 2008 2:27 pm

Basic

Post by adi5402 »

What am I doing wrong here

Code: Select all

$sqlStatusList = 'SELECT *'
        . ' FROM `status` ';
        
$statusList= mysql_query($sqlStatusList) or die(mysql_error());
$rowTest =mysql_num_rows($statusList);
        
echo $rowTest;      
$statusCount =0;
while($row =mysql_fetch_array($statusList)) {
 
$statusCount = $statusCount+1;
 
 
$statusList = $row['status'];
 
echo $statusList;
 
 
}
I keep getting an error that the mysql_fetch_array is not ok.

thanks
adi
User avatar
Frozenlight777
Forum Commoner
Posts: 75
Joined: Wed May 28, 2008 12:59 pm

Re: Basic

Post by Frozenlight777 »

Change $statuslist in the while loop to something different, it's the same name as the query.
adi5402
Forum Newbie
Posts: 10
Joined: Mon Jun 23, 2008 2:27 pm

Re: Basic

Post by adi5402 »

doesent the argument in the fetch funchtion have to pass the query.

I tried changeing it but no luck.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: Basic

Post by deejay »

$sqlStatusList = 'SELECT *'
. ' FROM `status` ';

that doesn't look right to me, but I'm no expert.
you could try something like

Code: Select all

 
 
$query = "SELECT * FROM table  ";
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Basic

Post by califdon »

adi5402 wrote:doesent the argument in the fetch funchtion have to pass the query.

I tried changeing it but no luck.
As Frozenlight777 said, you have used the same variable name, $statusList, twice for 2 different things: once for the results from the query, then later in your while loop for the value in one field ('status'), thus overwriting the variable and causing the next execution of the loop to have an invalid reference to the query results.

This is a good reason to use standard variable names, such as:

Code: Select all

$sql = 'SELECT *'
         . ' FROM `status` ';
        
$result = mysql_query($sql) or die(mysql_error());
$rowTest =mysql_num_rows($result);
echo $rowTest;     
$statusCount =0;
 
while($row =mysql_fetch_array($result)) {
    $statusCount++;
    $statusList = $row['status'];
    echo $statusList;
}
I assume you're doing this as a learning exercise, because there would be no reason to actually count rows this way, when you already have the number of rows from the php function. I also used the more convenient notation to increment the $statusCount variable.
adi5402
Forum Newbie
Posts: 10
Joined: Mon Jun 23, 2008 2:27 pm

Re: Basic

Post by adi5402 »

that worked thanks guys. I guess i confused myself witht the funny notations.

Thanks all you guys for your input.

I really appriciate it.

Regards
Adi
Post Reply