dongli7236 2013-07-19 09:46
浏览 53
已采纳

在php join中具有相同名称的Echo变量

I have a mysql join which is pulling from two tables name product & cart and they're both being pulled from a variable $row_checkout

If i have to echo a certain field, i can normally go $row_checkout['cartid'] and that works fine.

However, i have a coloumn in each table which is called the same 'Status'.

How do i echo from one of the tables? I thought something like $row_checkout['cart.status'] might work but it doesnt appear to?

My database code is as follows:

$colname_checkout = "-1";
if (isset($row_booking['sessionid'])) {
  $colname_checkout = (get_magic_quotes_gpc()) ? $row_booking['sessionid'] : addslashes($row_booking['sessionid']);
}
mysql_select_db($database_main, $main);
$query_checkout = sprintf("SELECT * FROM cart, productdatabase WHERE cart.productid = productdatabase.productid AND cart.status != 1 AND cart.status != 0 AND cart.sessionid = '%s' ORDER BY `name` ASC", $colname_checkout);
$checkout = mysql_query($query_checkout, $main) or die(mysql_error());
$row_checkout = mysql_fetch_assoc($checkout);
$totalRows_checkout = mysql_num_rows($checkout);
  • 写回答

2条回答 默认 最新

  • dongzhijing8202 2013-07-19 09:51
    关注

    You can use alias to change a tables field name, to a name that you want.

    the cart.status filter, you could make simpler, simpler by asking > 1

    SELECT c.status as car_status, pro.status as pro_status 
    FROM cart as c, productdatabase as pro
    WHERE c.productid = pro.productid AND c.status >1 AND c.sessionid = '%s' 
    ORDER BY `name` ASC", $colname_checkout
    

    Seeing what you get, it will display an associative array with all names and values, that you can use to address the data

    while ($row_checkout = mysql_fetch_assoc($checkout)) {
        print_r($row_checkout);
    }
    

    or specific the fields:

    while ($row_checkout = mysql_fetch_assoc($checkout)) {
        echo $row_checkout["car_status"];
        echo $row_checkout["pro_status"];
    }
    

    another comment, the mysql function is not recommended anymore. You could use MySQLi or PDO_MySQL. They are both object oriented and may need a little more time to learn.

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

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化