Erkdance
Erkdance

Reputation: 21

Storing db value into php array with while loop?

I am wanting to search through AdvInfo table and store the primary keys if Unit = Child.

 $result = mysqli_query($conn, "SELECT * FROM AdvInfo WHERE Unit = Child");     
 while ($row = mysqli_fetch_array($result, SQLSRV_FETCH_ASSOC))
 {

$childhbc = array_merge($childhbc, $row[0]);
echo $childhbc[0];
 }

Upvotes: 2

Views: 2731

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 157896

Keep it simple, man

$childhbc = array();
$result = mysqli_query($conn, "SELECT * FROM AdvInfo WHERE Unit = Child");     
while ($row = mysqli_fetch_row($result))
{
    $childhbc[] = $row[0];
}

The less intricate words you use - the better your program works

Upvotes: 1

Sylvain Leroux
Sylvain Leroux

Reputation: 52000

The second argument to array_merge should be an array. But to quote the documentation:

mysqli_fetch_array [...] returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in resultset.

In your code, $row is an array. $row[0] is a string.

while ($row = mysqli_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
    $childhbc = array_merge($childhbc, $row);
    echo $childhbc[0];
}

Upvotes: 1

Related Questions