Problem getting rows from DB zZzZZZzZZZzZZ

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
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Problem getting rows from DB zZzZZZzZZZzZZ

Post by Perfidus »

After a night with no sleeping at all, I'm starting to make some stupid mistakes, I'm trying to store the arrays ($row) coming from database in a new longer array called $despdef:

Code: Select all

$desp = mysql_num_rows($rs);
if(($desp<15)&&($desp>0)){
$desp2=15;
for($f=0;$f<$desp;$f++){
while ($row = mysql_fetch_array($rs)){
$despdef[$f]=$row;
}}
for($d=$desp;$d<$desp2;$d++){
$despdef[$d]=0;
}
}
The fact is that I only get 1 row from DB, I know is obvious,
but I'm too
o
o
o
tired...
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

I'm afraid a posted this question in the wrong place, I have already post it in Databases and I would like to delete it from here, but, how to?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

mysql_fetch_array() only returns data from one row.

You need to get each row in turn within your loop then run mysql_fetch_array() :wink:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Perfidus wrote:I'm afraid a posted this question in the wrong place, I have already post it in Databases and I would like to delete it from here, but, how to?
It's OK it's a PHP question more than a database question anyway.... it relates to your loop and what you're asking each repetition to do...
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

But, what's doing the "while" there?:

Code: Select all

for($f=0;$f<$desp;$f++){
while ($row = mysql_fetch_array($rs)){
$despdef[$f]=$row;
}}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Missed that bit sorry :oops:

Busy working and reading posts at the same time - doesn't mix :P

I'll look again
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Perfidus wrote:

Code: Select all

for($f=0;$f<$desp;$f++)
{while ($row = mysql_fetch_array($rs))
{$despdef&#1111;$f]=$row;}}
So you asked What does while do here???
The answer is very simple and easy to overlook --- All it does is that everytime a new array is returned from database, it stores tht array into same array position as before. All this continues until all rows are returned. then again it continues for next iteration of for loop. So if there are 15 records in the db you are fetching 225 rows of data. :!:

EDIT: I see you have been adequately answered by timvw in your other post...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd seriously suggest working on coding style.. yikes. 8O
Post Reply