2

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

2
  • 4
    You're going to need to be more specific about how to want to display the results; technically vardump($array); answers your question Commented Aug 17, 2010 at 20:23
  • 2
    Please comment on or update your original question to clarify it, rather than creating a new question. Commented Aug 17, 2010 at 20:32

6 Answers 6

2

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

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

1 Comment

var_dump() is a better alternative as print_r() can lie sometimes if you will.
2

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.

Comments

1
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.

Comments

0

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

$array = new array(); while($row = mysql_fetch_assoc($result)){ $array[] = $row; } return $array; 

1 Comment

that worked great :) thanks alot but when i try to display the result in an other function its showing me 1- Array 2- Array instead if the information in the array
0

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... } } 

Comments

0

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.

2 Comments

I feel like there has to be a better alternative to the if($header) that the while loop has to compute every iteration.
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.