This topic is locked
[SOLVED]

What is wrong in the code?

3/22/2024 2:50:08 AM
PHPRunner General questions
I
I author

Hello, with a code snippet in the designer I am trying to make visible the contents of the names in a table with the code below:

$names = array();
$rs = DB::Query('SELECT name FROM mytable');
while( $data = $rs->fetchAssoc() )
{
$names[] = $data["name"];
}
echo $names;

Unfortunately the names are not shown, what is wrong in the code? Thanks.

fhumanes 3/22/2024

Hello,

I do not know what you want to do with this code, but what is wrong is the use of the "echo" sentence to show the data of an "array".

Greetings,
fernando

dageciDevClub member 3/22/2024

I agree with Fernando,

instead of echo $names;

you should iterate through the array and print each element, something like this:

foreach ($names as $name) {
echo $name . " ";
}
admin 3/22/2024

Better yet, use
print_r($names);

In either case, you need to tell us what exactly it prints,