0

I have multiple tables named like so MOM2016, MOM2017, MOM2018.

When i run query in phpmyadmin

SHOW TABLES LIKE 'MOM%'

it returns 3 items as expected.

BUT!!!! When i run in php, my code seem to give me only 1 item in the array (first one only MOM2016).

$sql = "SHOW TABLES LIKE 'MOM%'";
$result = $conn->query($sql);
$dbArray = $result->fetch_assoc();
echo "DEBUG:".count($dbArray);

This give:

DEBUG:1

My php code is wrong? Pls help.

3
  • that is an array, holding one result, i.e. 1 Commented Mar 15, 2018 at 0:30
  • ok i will rephrase question. Commented Mar 15, 2018 at 0:32
  • you don't need to, you need to iterate through your result set Commented Mar 15, 2018 at 0:33

2 Answers 2

1

If you want to get all the results at once,

$dbArray = $result->fetch_all();
echo "DEBUG:".count($dbArray);
Sign up to request clarification or add additional context in comments.

3 Comments

I get 3 count too. But how do I access the items in array? i tried print dbArray[0] and it prints "Array" only.
fetch_all will return an array of arrays, so the table names are in $dbArray[0][0], $dbArray[1][0], $dbArray[2][0]. The same applies to the other poster's answer.
I guess this is basic stuff that I do not know. Thank you this works now. Thanks for your help and have a nice day.
1

Iterate through your fetch resource

$dbArray = array();
while ($row = $result->fetch_assoc()) {
   $dbArray[] = $row;
}

print "DEBUG: " . count($dbArray);

1 Comment

by this way i get count 3. but the content of dbArray is [Array, Array, Array].

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.