...

View Full Version : How do I extract 2 rows ?



jeddi
04-30-2011, 08:44 AM
Hi,

I am using the LIMIT to grab just 2 rows from my table,
but I am not sure how I should get the data into my variables.

In the code below you can see that I am trying to get the
product code, title and description in to the variables.

But I don't know how to take thr first row and then afterwards the second row :confused:

I also need to check that there IS a second row.

Would appreciate some help. :)

This is the code I have :



$sql = "SELECT id, prod_id, prod_title, prod_desc From pdc_promos WHERE promo_id = '$the_cntr' ORDER BY promo_id LIMIT 2";
$result = mysql_query($sql) or die("could not select pdc_promos");

if(mysql_num_rows($result) > 0 ){
$row = mysql_fetch_assoc($result); //

$promo1 = $row['prod_id'];
$pro_title_1 = $row['prod_title'];
$pro_desc_1 $row['prod_desc'];

$promo2 = $row['prod_id'];
$pro_title_2 = $row['prod_title'];
$pro_desc_2 $row['prod_desc'];




.

bullant
04-30-2011, 08:56 AM
You're just about there. All you need to do is loop through the result set extracting a new row of data on each iteration


while($row=mysql_fetch_assoc($rs)){
$promo = $row['promoId'];

//do something with $promo on this iteration
}But do you want to do something with each row of data in the result set on each iteration of the loop or do you want to create a 2D array containing the rows of data in the result set?

jeddi
04-30-2011, 11:55 AM
Thanks for reply,

I am just trying to get the product code, title and description in to these
variables:

$promo1
$pro_title_1
$pro_desc_1

$promo2
$pro_title_2
$pro_desc_2

OR,

I could use little 2-element-arrays:

$promo()
$pro_title()
$pro_desc()

Just not sure how I would fill them.


.

bullant
04-30-2011, 12:01 PM
Thanks for reply,

I am just trying to get the product code, title and description in to these
variables:

$promo1
$pro_title_1
$pro_desc_1

$promo2
$pro_title_2
$pro_desc_2



ok, every time mysql_fetch_assoc($rs) is executed. it moves the $rs internal pointer to the next row in $rs

So you could do something like this.


$row=mysql_fetch_assoc($rs);
$promo1 = $row['promoId'];
$row=mysql_fetch_assoc($rs);
$promo2 = $row['promoId'];

jeddi
04-30-2011, 12:45 PM
Yer,

I usually use the while loop but:

I guess since I know there will only be 2 results,
this is a reasonable method.

Thanks

bullant
04-30-2011, 01:03 PM
no problem :)

or you could also use a while loop and put the column data into individual arrays for each column or into one 2D array.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum