By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

Need help to get more than row at the same time

P: 34
Hi can anyone help me, I need to fetch more than 1 row at the same time. I've used print_r(mysql_fetch_array($result)); but I only get 1 row is there away of of getting more then 1 row.
kind regards
Zawadi
Jul 23 '07 #1
Share this Question
Share on Google+
9 Replies


nathj
Expert 100+
P: 938
Hi can anyone help me, I need to fetch more than 1 row at the same time. I've used print_r(mysql_fetch_array($result)); but I only get 1 row is there away of of getting more then 1 row.
kind regards
Zawadi
Hi,

Try something like this:
Expand|Select|Wrap|Line Numbers
  1. while ($row = mysql_fetch_array($result, MYSQL_BOTH))
  2.         {
  3.             if ($row)
  4.             {
  5.                  echo $row;
  6.             }
  7.         }
  8.  
This should print each row in the result of the query. If this only ever returns 1 row of data then perhaps that's all that was returned? Check the SQL

Cheers
nathj
Jul 23 '07 #2

P: 34
Hi,

Try something like this:
Expand|Select|Wrap|Line Numbers
  1. while ($row = mysql_fetch_array($result, MYSQL_BOTH))
  2.         {
  3.             if ($row)
  4.             {
  5.                  echo $row;
  6.             }
  7.         }
  8.  
This should print each row in the result of the query. If this only ever returns 1 row of data then perhaps that's all that was returned? Check the SQL

Cheers
nathj
Thanks this really did help, but the output is Array Array Array. How do i get the data from the arrays.
Thanks for your reply
zar
Jul 23 '07 #3

mwasif
Expert 100+
P: 801
MYSQL_BOTH is the default value of mysql_fetch_array(). Try the below code

[PHP]while ($row = mysql_fetch_array($result))
{
echo $row["your_column_name_here"];
}[/PHP]
Jul 23 '07 #4

P: 34
Thanks but I must be doing something wrong all I'm getting is the column data in a string and what i need is all the rows i'm calling set in an array. I know i have 3 rows of data and I need them to look like this, but all 3 rows.

Array
(
[0] => 1
[id] => 1
[1] => test
[name] => test
[2] => 86
[subtotal] => 86
[3] => t541
[identnum] => t541
[4] => 1
[itemqut] => 1
[5] => Vinyl camera case
[itemproduct] => Vinyl camera case
[6] => None
[itemsize] => None
[7] => 57.00
[itemprice] => 57.00
)
but I'm only getting 1 row,

if you can help I would be very greatful
kind regards
Zar
Jul 23 '07 #5

mwasif
Expert 100+
P: 801
Please post your code.
Jul 23 '07 #6

P: 34
this line tells database which rows to look at the one with same username $glog is a var of the user name sent by the user.
$query = "SELECT * FROM theOrder WHERE name ='$glog'";

this is the query
$result = mysql_query($query);

the number of rows from the query
$numR = mysql_num_rows($result);

the array data this is good but it only gives me the 1st row, and like i said there are 3 row using the same user name and i need all 3 rows set out in a array
print_r(mysql_fetch_array($result));

Could you tell what I have done wrong

kind regards
Zar
Jul 23 '07 #7

mwasif
Expert 100+
P: 801
Did you use while() as nathj suggested? Look at the below code.
[PHP]$query = "SELECT * FROM theOrder WHERE name ='$glog'";

$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
print_r($row);
}[/PHP]
Jul 23 '07 #8

P: 34
Thank you, thank you thank, I must have done something stupid your my savior

Zar
Jul 23 '07 #9

nathj
Expert 100+
P: 938
One final point, if I may. Check out the article on building a data abstraction layer. It'll really help with this sort of stuff and it will properly modularise all your code for you making it easier to maintain and re-use.

Cheers
nathj
Jul 24 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.