drz73366 2014-08-20 17:50
浏览 48
已采纳

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

  • 写回答

3条回答 默认 最新

  • dqsvf28682 2014-08-26 08:27
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考