Reputation: 1779
php foreach echo prints "Array" as value asks this question but thats not the case here
try {require_once'libs/config.php';
$con = new PDO($dsn, $user, $pass, $opt);
$query = "SELECT * FROM table ";
//first pass just gets the column names
print "<table> \n";
$result = $con->query($query);
//return only the first row (we only need field names)
$row = $result->fetch(PDO::FETCH_ASSOC);
print "<thead> <tr> \n";
foreach ($row as $field => $value){
print " <th>$field</th> \n";
} // end foreach
print " </tr> </thead> <tbobdy> \n";
//second query gets the data
$data = $con->query($query);
$data->setFetchMode(PDO::FETCH_ASSOC);
foreach($data as $row){
echo $row; // prints Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array Array
print " <tr> \n";
foreach ($row as $name=>$value){
print "<td>$value</td>\n"; }
} // end field loop
print " </tr> \n";
} // end record loop
// print "</tbody> </table> \n";
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
} // end try
$con = null;
?>
Upvotes: 0
Views: 2390
Reputation: 3698
So simple that means $row is an array not an variable. You need to do print_r($row);
instead of echo $row;
Upvotes: 1
Reputation: 1779
i think i got it foreach as ..
could not echo a full array its either key or value or both, if it's a array it prints 'array' overhere since $data contains a array the full row it just prints 'array',
which is true by every multi-dimensional array as you can see the following example from the php docs
/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";
foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}
the result from a database is a multi-dimensional array
Upvotes: 0
Reputation: 602
You need to print $value
in the foreach
loop as it contains the values of the array elements.
Your final output will be something like this:
<?php
try {
require_once 'libs/config.php';
$con = new PDO($dsn, $user, $pass, $opt);
$query = "SELECT * FROM table ";
//first pass just gets the column names
print "<table> \n";
$result = $con->query($query);
//return only the first row (we only need field names)
$row = $result->fetch(PDO::FETCH_ASSOC);
print "<thead> <tr> \n";
foreach ($row as $field => $value) {
print " <th>$value</th> \n";
} // end foreach
print " </tr> </thead> <tbobdy> \n";
//second query gets the data
$data = $con->query($query);
$data->setFetchMode(PDO::FETCH_ASSOC);
foreach ($data as $Somefield => $SomeValue) {
echo $SomeValue; // prints your disered output
print " <tr> \n";
foreach ($row as $name => $value) {
print "<td>$value</td>\n";
}
} // end field loop
print " </tr> \n";
} // end record loop
// print "</tbody> </table> \n";
catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
} // end try
$con = null;
?>
?>
Upvotes: 0