Page 1 of 1

Basic

Posted: Mon Jun 23, 2008 2:30 pm
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

Re: Basic

Posted: Mon Jun 23, 2008 3:08 pm
by Frozenlight777
Change $statuslist in the while loop to something different, it's the same name as the query.

Re: Basic

Posted: Mon Jun 23, 2008 3:45 pm
by adi5402
doesent the argument in the fetch funchtion have to pass the query.

I tried changeing it but no luck.

Re: Basic

Posted: Mon Jun 23, 2008 3:52 pm
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  ";

Re: Basic

Posted: Mon Jun 23, 2008 4:29 pm
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.

Re: Basic

Posted: Tue Jun 24, 2008 12:53 am
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