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

时间:2014-08-20 17:50:47

标签: php mysql sql

我使用连接从各种表中检索数据并且我成功了但是当我想要检索它时,查询只显示最后一行的结果,即使我在它上面使用while循环但是它不能正常工作正确地给我一个属于最后一行的结果。 这是我的php脚本。

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;
    }
}

但数组包含此数据。它有更多类似列的结果。

  

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 )

当我在sql中运行此查询时,它返回两行,所以如何在我的php脚本中相应地检索数据我真的很困惑,请帮助我。 现在为了进一步了解我正在添加这张图片。而且你们可以看到这个查询返回了两行,所以如何在我的php脚本中单独获取这两行并在我的页面上显示它们。 sql picture

3 个答案:

答案 0 :(得分:1)

你有这样的陈述:

$row = mysql_fetch_array($sql);

这将取得"首先"从结果集(如果至少有一行)。

然后你有一个while循环,它会进行另一次获取:

$row1 = mysql_fetch_array($sql)

这将得到第二个"以及后续行。 (因为您已经检索了第一行。)

如果你不想"跳过"第一行,然后从代码中删除此行:

$row = mysql_fetch_array($sql);

答案 1 :(得分:1)

问题是你是从同一个变量中获取结果,这会导致你出现逻辑错误,看看你在结果中获取结果并将其存储在$row中,这样就可以获取结果集中的第一行再次提取记录并存储在另一个变量中,但是来自同一个变量$sql并在结果中应用while循环,因为第一行被跳过,所以只得到一行。

解决方案:如果您确实需要,请不要从同一个变量中获取结果另一个变量并在其中存储相同的查询

$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'];
    }

我希望这会奏效。

答案 2 :(得分:0)

似乎只是获得最后一行结果的逻辑。 如果在While循环内,你放"echo 'in the loop';",你可能会看到两次。

但正如你所做的那样:

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

首次使用$product_ids[]填充$row1['pro_id'],然后在下一个循环中,使用新值重新填充它,销毁第一个。

事实上,你犯了一个常见错误,我想我明白你想做什么:

  1. 循环到getresult
  2. 显示结果
  3. 使用

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

    但是你把foreach放在while循环中,因为它必须在外面。 最简单明了的方法是:

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

    详细信息:使用其他MySQL驱动程序,例如mysqlimysql基本驱动程序是偶像。