I have a query result set that I want to put into a PHP array variable using mysqli_fetch_array but I don’t know the number of columns in the query until runtime.
I tried this approach to no avail
while ($this->row = mysqli_fetch_array($this->qr_list_users))
{
$this->users = array(
for ($this->x = 0; $this->x < ($this->numcols - 1); ++$this->x)
{
“$this->colname[” . $this->x] “]=>$this->row[$this->colname[” . $this->x] . “],”
}
“$this->colname[” . $this->x] “]=>$this->row[$this->colname[” . $this->x] . “])”
}
It works fine if I hard code a line for each column. So how can I modify the array assingment at runtime?
Excellent, that works great! Thanks for the help.
telos
June 28, 2010, 6:11pm
4
That’s what I thought. Try:
while ($user = mysqli_fetch_assoc($result)) $users[] = $user;
print_r($users);
Check out http://php.net/mysqli_fetch_assoc
telos
June 28, 2010, 6:03pm
5
Can you give me an example of what you’re wanting the array to look like after it’s done?
Besides that fact that your code has tons of syntax errors, I’m guessing there is a much easier way to do what you want.
I want it to look like this, but I don’t know the column names until runtime
while ($row = mysqli_fetch_array($qr_list_users))
{
$users = array(
‘user_id’ => $row[‘user_id’],
‘email’ => $row[‘email’],
‘user_name’ => $row[‘user_name’],
‘zip_code’ => $row[‘zip_code’],
‘age’ => $row[‘age’]);
sorry, add the closing } bracket for the while construct