Вопрос

I'm having a bit of an issue with what is normally a simple foreach loop through a query array. I am trying to target individual field values from a row, but its only returning the first letter of each. New to OCI so any help would be appreciated.

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

    }
}
Это было полезно?

Решение

The while is looping over all returned rows. You are then foreach ing across that row and accessing the columns in the row (don't do that). You just want the while and access CART_NAME in $row:

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

If you want to store the rows in an array to use more than once in the script, then:

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

//now or later

foreach ($row as $item) {
    echo $item['CART_NAME'];
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top