1

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?

4 Answers 4

2

You need to use mysqli_fetch_assoc inside of a while() loop:

$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))
{
    // $row now contains an array with the `COLUMN_NAME` inside.
}
Sign up to request clarification or add additional context in comments.

Comments

1

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
}

1 Comment

Upvoted all answers, but accepting this one as it caught me first and explained sufficient for me. Thanks @Alive to Die.
1

using loop you can access all values

 $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);
 }

Comments

1

To get all the values echo'ed, you need to use while() loop 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($rows = mysqli_fetch_assoc($result)) {
    print_r($rows); // this contains all the necessary data that you are looking for...
}

Here is reference to mysqli_fetch_assoc()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.