Page 1 of 1
Problem getting rows from DB zZzZZZzZZZzZZ
Posted: Mon Apr 04, 2005 5:46 am
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...
Posted: Mon Apr 04, 2005 6:02 am
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?
Posted: Mon Apr 04, 2005 6:14 am
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()

Posted: Mon Apr 04, 2005 6:18 am
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...
Posted: Mon Apr 04, 2005 6:22 am
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;
}}
Posted: Mon Apr 04, 2005 6:25 am
by Chris Corbyn
Missed that bit sorry
Busy working and reading posts at the same time - doesn't mix
I'll look again
Posted: Mon Apr 04, 2005 7:06 am
by n00b Saibot
Perfidus wrote:
Code: Select all
for($f=0;$f<$desp;$f++)
{while ($row = mysql_fetch_array($rs))
{$despdefї$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...
Posted: Mon Apr 04, 2005 9:38 am
by feyd
I'd seriously suggest working on coding style.. yikes.
