Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    How do I extract 2 rows ?

    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

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

    Would appreciate some help.

    This is the code I have :

    PHP Code:
    $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) > ){   
      
    $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']; 

    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    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
    PHP Code:
    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?
    Last edited by bullant; 04-30-2011 at 07:59 AM.

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    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.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by jeddi View Post
    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.

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





  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    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
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #6
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •