Neo Neo - 1 month ago 7
PHP Question

mysqli query result is displayed as"Array" in php. How to retrieve the individual values from it?

Below is my code to retrieve the column names from the table "COLUMNS" of database "INFORMATION_SCHEMA".

$result = mysqli_query($con,"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CustomersTable'") or die(mysqli_error($con));

$columns = $result;
foreach ($columns as $column){
echo $column;
}


what I get echoed is
ArrayArrayArrayArrayArrayArrayArrayArray
. However, if I write

mysqli_fetch_assoc($result);


instead of

$columns = $result;


then I get only the first value of the array echoed.

How do I get all the values echoed as I expect it to be?

Answer Source

Use while() loop along with mysqli_fetch_assoc:-

$result = mysqli_query($con,"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CustomersTable'") or die(mysqli_error($con));

while($row = mysqli_fetch_assoc($result)){
  print_r($row);//array contains `COLUMN_NAME`.
  //based on printed value you can get how to do echo easily
}