user3400389
user3400389

Reputation: 367

Store data into array variable php

If i echo inside the while loop i get 4,2 values and if i echo outside the while loop then i only get 2. I want to get the data from $row into the values array. is something missing in this code?

$query = oci_parse($con, "SELECT count(*) FROM Counter GROUP BY Blog_name");
oci_execute($query);

while($row = oci_fetch_array($query))
{
    $s = $row[0].',';
    $values = explode(',', $s);
    echo $values[0]; // 4
    echo $values[1]; // 2
}
echo $values[0]; // 2
echo $values[1]; // 2

Upvotes: 0

Views: 5118

Answers (4)

hellosheikh
hellosheikh

Reputation: 3015

Do like this:

$s = array();
while($row = oci_fetch_array($query))  {
    $s[] = $row[0];
}

echo $s[0];
print_r($s);

Upvotes: 1

Krish R
Krish R

Reputation: 22711

Try this,

$Values = array();
 while($row = oci_fetch_array($query))
{
    $Values[] = $row[0];       
}
 echo $Values[0];

Upvotes: 2

Mohsin Inayat Khan
Mohsin Inayat Khan

Reputation: 1121

$query = oci_parse($con, "SELECT count(*) FROM Counter GROUP BY Blog_name");
oci_execute($query);

$values = array();
while($row = oci_fetch_array($query))
{
    $values[] = $row[0];
}

print_r($values);

Upvotes: 0

mable
mable

Reputation: 144

First, $values as you're using it is just a string, and is not an array.

Try adding before the while loop.

$values = array();

Second, there's really no need to use explode here, it's an unnecessary step. If you only want the first column in the row, you can simply add that column to $values. (Currently you are overwriting the contents of $values at each iteration of the loop because you are missing the [].)

$values[] = $row[0];

If you're trying to put ALL of the columns in each row into values, try:

$values[] = $row;

If you're trying to individually put the contents of each column into it's own index in $values, try:

while($row = oci_fetch_array($query))
{
    foreach($row as $column)
    {
        $values[] = $column;
    }
}

Upvotes: 2

Related Questions