只返回每行值的第一个字母

时间:2014-03-27 18:47:12

标签: php sql oracle oracle10g oci

我对通常是通过查询数组的简单foreach循环有一点问题。我试图从一行中定位单个字段值,但它只返回每个字符的第一个字母。 OCI的新手,所以任何帮助都会受到赞赏。

$custcart = oci_parse($conn,"SELECT * FROM A113222813_CARTLINE WHERE CUST_ID=  
:cust_id");
oci_bind_by_name($custcart, ":cust_id", $_SESSION["CUST_ID"]);

if (!$custcart) {
    echo "There was an error. Please wait wwhile we redirect you.";
    $url ="login.php";
    $time_out = 5;
    header("refresh: $time_out; url=$url");
}

oci_execute($custcart);
include 'includes/header.php';

while ($row = oci_fetch_array($custcart, OCI_ASSOC)) {
    foreach ($row as $item) {
        echo $item['CART_NAME'];

    }
}

1 个答案:

答案 0 :(得分:1)

while循环遍历所有返回的行。然后,您foreach穿过该行并访问该行中的列(不要这样做)。您只需要while中的CART_NAME并访问$row

while ($row = oci_fetch_array($custcart, OCI_ASSOC)) {
    echo $row['CART_NAME'];
}

如果要将行存储在数组中以在脚本中多次使用,则:

while ($row[] = oci_fetch_array($custcart, OCI_ASSOC)) { }

//now or later

foreach ($row as $item) {
    echo $item['CART_NAME'];
}