php mysql使用join显示最后一行结果检索多个数据

I have use joins to retrieve the data from various tables and I am successful in it but when I want to retrieve it the query only show me the result of last row even though I have use while loop on it but its not working properly give me the one result only which belongs to last row . Here is my php scrip.

include("connections/conn.php");
$sql = mysql_query("SELECT * FROM `tbl_cart` as c join " . 
                                  "`tbl_product` as p join " . 
                                  "`tbl_catagory` as ca join " . 
                                  "`tbl_compony` as co join " . 
                                  "`tbl_images` as i " . 
                                  "on c.pid=p.pro_id " . 
                                  "and p.compony=co.com_id " . 
                                  "and p.catagory=ca.cat_id " . 
                                  "and p.pro_id=i.p_id");   
$row = mysql_fetch_array($sql);
while($row1 = mysql_fetch_array($sql)) {
    $product_ids[]=$row1['pro_id'];
    foreach($product_ids as $pro) {
        echo $pro;
    }
}

but the array contains this data. which have more result of from similar columns.

111Array ( [0] => 33 [cart_id] => 33 [1] => 110 [pid] => 110 [2] => 20 [quantity] => 20 [3] => 65,000 [price] => 65,000 [4] => 9 [userid] => 9 [5] => 2014-08-14 [dated] => 2013-08-24 [6] => 110 [pro_id] => 110 [7] => Aspire S3-391 [title] => Aspire S3-391 [8] => 10.68 lbs [weight] => 10.68 lbs [9] => 15.90 inch [width] => 15.90 inch [10] => 2.31 cm [height] => 2.31 cm [11] => Black [color] => Black [12] => 65,000 [13] => $1499 [USD] => $1499 [14] => [waranty] => [15] => Make work a touch easier.Get in touch with your productive side. Tap and scroll your way through the workday with an intuitive touch screen (select models) that helps you work so much smarter. Count on durability inside and out from built in security to the spill resistant keyboard and a thin light design thats packed with style. [description] => Make work a touch easier.Get in touch with your productive side. Tap and scroll your way through the workday with an intuitive touch screen (select models) that helps you work so much smarter. Count on durability inside and out from built in security to the spill resistant keyboard and a thin light design thats packed with style. [16] => 1 [catagory] => 1 [17] => 4 [compony] => 4 [18] => Intel® Core™ i5 [l_ptype] => Intel® Core™ i5 [19] => 2.8 GHZ [l_pspeed] => 2.8 GHZ [20] => 700 GB [l_harddisk] => 700 GB [21] => 4GB DDR3 [l_RAM] => 4GB DDR3 [22] => Windows 8 [l_op] => Windows 8 [23] => [l_battery] => [24] => [lcd_brightness] => [25] => [lcd_contrast] => [26] => [lcd_imagesize] => [27] => [lcd_pixelsize] => [28] => [lcd_resolution] => [29] => [lcd_backlight] => [30] => [lcd_depth] => [31] => [p_pspeed] => [32] => [p_resolution] => [33] => [p_pconsumption] => [34] => [p_memorycapacity] => [35] => [p_storagecapacity] => [36] => [p_interface] => [37] => [p_processorspeed] => [38] => [pro_p_consumption] => [39] => [pro_brightness] => [40] => [pro_resolution] => [41] => [pro_imagesize] => [42] => [pro_ptype] => [43] => [pro_imagesignal] => [44] => [pro_contrast] => [45] => [sca_capacity] => [46] => [sca_speed] => [47] => [sca_colordepth] => [48] => [sca_usb] => [49] => [sca_documentsize] => [50] => [sca_resolution] => [51] => 2013-08-24 [52] => 1 [cat_id] => 1 [53] => Laptop [cat_title] => Laptop [54] => 4 [com_id] => 4 [55] => Acer [com_title] => Acer [56] => 120 [img_id] => 120 [57] => Aspire S3-391_main.jpg [main_image] => Aspire S3-391_main.jpg [58] => Aspire S3-391_left.jpg [left_image] => Aspire S3-391_left.jpg [59] => Aspire S3-391_right.jpg [right_image] => Aspire S3-391_right.jpg [60] => Aspire S3-391_top.jpg [top_image] => Aspire S3-391_top.jpg [61] => Aspire S3-391_other.jpg [other_image] => Aspire S3-391_other.jpg [62] => 110 [p_id] => 110 )

And when I run this query in sql it returns me two rows so how can I retrieve the data accordingly in my php script I am really confuse in it please help me out . Now for further understanding i am adding this picture. And you people can see that this query is returning me two rows so how will fetch the two rows separately in my php script and show them on my page. sql picture

The problem is you are fetching the results from the same variable which bring you to a logical error see what's happening here you fetch once the result and store it in $row so first row from the result set get fetched and again your fetching the record and storing in another variable but from the same variable $sql and applying the while loop in result you are getting only one row because the first row get skipped.

Solution: Do not fetch the Results from the same variable make another variable and store the same query in it if you really needed e.g

$sql = mysql_query("SELECT * FROM `tbl_cart` as c join " . 
                                  "`tbl_product` as p join " . 
                                  "`tbl_catagory` as ca join " . 
                                  "`tbl_compony` as co join " . 
                                  "`tbl_images` as i " . 
                                  "on c.pid=p.pro_id " . 
                                  "and p.compony=co.com_id " . 
                                  "and p.catagory=ca.cat_id " . 
                                  "and p.pro_id=i.p_id");   

$sql1 = mysql_query("SELECT * FROM `tbl_cart` as c join " . 
                                  "`tbl_product` as p join " . 
                                  "`tbl_catagory` as ca join " . 
                                  "`tbl_compony` as co join " . 
                                  "`tbl_images` as i " . 
                                  "on c.pid=p.pro_id " . 
                                  "and p.compony=co.com_id " . 
                                  "and p.catagory=ca.cat_id " . 
                                  "and p.pro_id=i.p_id");   
$row    =   mysql_fetch_array($sql);
while($row1= mysql_fetch_array($sql1))
{
    echo $row1['pro_id'];
    }

I hope this will work.

You have this statement:

$row = mysql_fetch_array($sql);

That will fetch the "first" from the resultset (if there's at least one row).

Then you have a while loop, that does another fetch:

$row1 = mysql_fetch_array($sql)

That will get the "second" and following row. (Since you've already retrieved the first row.)

If you don't want to "skip" that first row, then remove this line from your code:

$row = mysql_fetch_array($sql);

Seems to be logicial to get only last row result. If inside the While loop, you put "echo 'in the loop';", you'll probably see that two times.

But as you do:

'$product_ids[]=$row1['pro_id'];'

you fill $product_ids[] first time with $row1['pro_id'] and, at next loop, you fill it again with the new value, destroying the first one.

In fact, you're making a common mistake and I suppose I understand what you want to do:

  1. loop to getresult
  2. display result

using

foreach($product_ids as $pro) {
    echo $pro;

But you have put the foreach inside the while loop as it must be outside. A most simple and understable way would be:

`$index=0;
while($row1 = mysql_fetch_array($sql))
{
    $product_ids[$index]=$row1['pro_id'];
    $index++;
}

foreach($product_ids as $pro) {
        echo $pro;
    }
}`

A detail: use another MySQL Driver, like mysqli as mysql basic driver is obsolote.