0

I want to output all records in my database. So far so good, but when I loop through it, php gives me an error " Array to string conversion ".

  • I added an index to the array but then it does just output obviously the first or secound ( etc. ) column.

    $conn = new PDO("mysql:host=localhost;dbname=database","root","");
    $stmt = $conn->prepare('SELECT * FROM de');
    $stmt ->execute();
    $result = $stmt ->fetchAll();
    
    if (is_array($result) || is_object($result))
    {
        foreach ($result[0] as $value)
            {
                echo "<table><tr><td>'$value'</td></tr></table>";
            }
    }
    

So, with the index, it does work. But I need all records, not just one.

I appreciate every comment and help!

2
  • It's foreach ($result as $value){} as you've have not determined that $result[0] is indeed an array. Commented Sep 12, 2016 at 11:35
  • foreach ($result as $value) should be like this Commented Sep 12, 2016 at 11:35

3 Answers 3

7

I would start with a nested loop. I also wonder if you want a table for every value

$conn = new PDO("mysql:host=localhost;dbname=database","root","");
$stmt = $conn->prepare('SELECT * FROM de');
$stmt ->execute();
$result = $stmt ->fetchAll();

if (is_array($result) || is_object($result))
{
    foreach ($result as $row){  //Go through every row in the result
        echo('<table>');
        foreach ($row as $value){    //Go through every value in the row
            echo "<tr><td>'$value'</td></tr>";
        }
        echo('</table>');
    }
}

This will print every row as a new table, but you can search out the variation you want.

Sign up to request clarification or add additional context in comments.

4 Comments

I love you :) Thank you verry much for your help! It did work. Btw, if you wouldn't mind to answer, why do I need two loops, to get all records?
You need two loops, because you need to get two separate things: 1) Every row from the results. And then 2) Every value from each row.
Of course. I wanted already but I've to wait 10 minutes to accept an answer
OK! I'm not going anywhere :-)
0

A nested foreach loop should work.

foreach ($result as $values)
{
  foreach ($values as $value) {
    echo "<table><tr><td>'$value'</td></tr></table>";
  }
}

4 Comments

That will just print 'Array Array Array' all down the screen. OP wants every value from every row.
Ah I see. The shouted "no" wasn't necessary though ;)
There! I've stopped shouting :-)
Hehe my feelings have now been unhurt. I'll stop crying now :-D
0

An alternative to nested loops, if you know the number of fields and said number is constant for each execution; A state that's found in most situations; You can use vsprintf () to print out the rows.

Quick example:

$result = {{'John', 'Doe'}, {'Sarah', 'Jane'}};
$output = '<table>';
foreach ($result as $row) {
   $output .= vsprintf ("<tr><td>%s</td><td>%s</td></tr>\n", $row);
}
echo $output."</table>";

Cuts down a bit on the code, and quite a bit on the code complexity. The template for the rows themselves can also be extracted to a variable (or template view), outside of the loop, to make the code even cleaner.

Comments