Home > Software design >  Php printing data from database [duplicate]
Php printing data from database [duplicate]

Time:09-30

I am trying to get the data from the database, but there is a problem printing it out

function getData($connection)
    {
        $query = "SELECT Email FROM SUBSCRIPTIONS";
        $result = $connection->query($query);
        return $result->fetch_all();
    }

$rows = $databaseOperations->getData($connection);
var_dump($rows);
foreach($rows as $row){
    echo('Data: ' . $row . "<br>");
}

And I am getting:

array(2) { [0]=> array(1) { [0]=> string(12) "sfddsffdsfsd" } [1]=> array(1) { [0]=> string(13) "fdsdffsdsfddf" } }
Warning: Array to string conversion in C:\Users\Raitis\Documents\\admin.php on line 47
Data: Array

Warning: Array to string conversion in C:\Users\Raitis\Documents\\admin.php on line 47
Data: Array

So, data is in the $rows, but as result, I get an array? String is essentially array of chars, but PHP shouldn't treat string as an array of chars, because printing it out would be hell.

CodePudding user response:

As the data is output as such:

[
    [[string]]
    [[string]]
]

You could try the following:

foreach($rows as $row){
    echo('Data: ' . $row[0] . "<br>");
}

CodePudding user response:

You cannot echo an Array! each $row inside $rows might or still represent an array

you can:

foreach($rows as $row){
    echo "\nData: \n";
    print_r($row);
    echo "<br>";
}

or:

foreach($rows as $row){
    echo('Data: ' . $row[0] . "<br>");
}

depending on the arrays built and response.

  • Related