tags:

views:

88

answers:

6

I am trying to build a function that extracts information from a database and inserts it into an associative array in PHP using mysql_fetch_assoc, and return the array so another function can display it. I need a way to display the returned assoc array. This should be a different function from the first one

A: 

This should get you going:

$rows = mysql_query("select * from whatever");
if ($rows) {
   while ($record = mysql_fetch_array($rows)) {
       echo $record["column1"];
       echo $record["column2"];
       // or you could just var_dump($record); to see what came back...
   }
}
Fosco
+1  A: 

print_r($array) will give nicely formatted (textually, not html) output.

fredley
@fredley var_dump() is a better alternative as print_r() can lie sometimes if you will.
Chris
@fredley or <pre> <?php print_r($array) ?></pre> for nicer formatting
ford
A: 

The following should work:

$rows = mysql_query("select * from whatever");
if ($rows) {
    $header = true;
    while ($record = mysql_fetch_assoc($rows)) {
        if ($header) {
            echo '<tr>';
            foreach (array_keys($record) AS $col) {
                echo '<td>'.htmlspecialchars($col).'</td>';
            }
            echo '</tr>';
            $header = false;
        }
        echo '<tr>';
        foreach (array_values($record) AS $col) {
            echo '<td>'.htmlspecialchars($col).'</td>';
        }
        echo '</tr>';
    }
}

(Yes, blatant mod of Fosco's code)

This should print the column headers once, then the contents after that. This would print just whatever columns were retrieved from the DB, regardless of the query.

Slokun
I feel like there has to be a better alternative to the if($header) that the while loop has to compute every iteration.
Chris
@Chris You could do it without having the check every time. Grab the row from the DB, check if you got it, print the headers, then do a do...while loop over it. However, this way is simpler to implement and read, and the check is O(1), and amounts for, in this case, 1/(4+(n*4)) computations per row, the result is negligible.
Slokun
+2  A: 

If you just want information about what is in the array (for debugging purposes), you can use print_r($array) or var_dump($array), or var_export($array) to print it in PHP's array format.

If you want nicely formatted output, you might want to do something like:

<table border="1">
  <?php
  foreach($array as $name => $value) {
    echo "<tr><th>".htmlspecialchars($name).
        "</th><td>".htmlspecialchars($value)."</th></tr>";
  }
  ?>
</table>

This will, as you might already see, print a nicely formatted table with the names in the left column and the values in the right column.

Frxstrem
Remember to encode the special chars.
Artefacto
+1  A: 
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $column => $value) {
        //Column name is in $column, value in $value
        //do displaying here
    }
}

If this is a new program, consider using the mysqli extension instead.

Artefacto
A: 

Assuming you've made the call, and got $result back:

$array = new array();
while($row = mysql_fetch_assoc($result)){
  $array[] = $row;
}
return $array;
fredley
that worked great :) thanks alotbut when i try to display the result in an other function its showing me 1- Array 2- Arrayinstead if the information in the array
Mohamed
okay i figured out thank you so much for your help
Mohamed