By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,189 Members | 2,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,189 IT Pros & Developers. It's quick & easy.

Result of Mysql Query in a PHP table

P: n/a
Hi,

I've a problem:

I want to have the result of my Mysql Query in a Table in my php file.
Now I've this:
<?

mysql_connect("localhost","root")
or die ("Keine Verbindung moeglich");

mysql_select_db("datenbank")
or die ("Die Datenbank existiert nicht");

$abfrage = "SELECT * FROM tabelle";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis))

{
echo $row->Referenznummer;
}

?>
But I want that the result is in a Table. With the heading "Referenznummer".
It should be like:
---------------
Referenznummer:
---------------
Result 1
---------------
Result 2
......
....
...
I searched a long time in google without success, please help me

Thanks
Felix

Btw: sorry for my bad english
Jul 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Felix wrote:
Hi,

I've a problem:

I want to have the result of my Mysql Query in a Table in my php file.
Now I've this:
<?

mysql_connect("localhost","root")
or die ("Keine Verbindung moeglich");

mysql_select_db("datenbank")
or die ("Die Datenbank existiert nicht");
echo "<table>";
echo "<tr><th>Referenznummer</th></tr>"; $abfrage = "SELECT * FROM tabelle";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis))

{ echo "<tr><td>"; echo $row->Referenznummer; echo "</td></tr>"; } echo "</table>";
?>
But I want that the result is in a Table. With the heading "Referenznummer".
It should be like:


Jul 16 '05 #2

P: n/a
Felix wrote:
mysql_connect("localhost","root")
or die ("Keine Verbindung moeglich");

mysql_select_db("datenbank")
or die ("Die Datenbank existiert nicht");

$abfrage = "SELECT * FROM tabelle";
$ergebnis = mysql_query($abfrage); But I want that the result is in a Table. With the heading "Referenznummer".
It should be like:
---------------
Referenznummer:
---------------
Result 1
---------------
Result 2


Hi, Felix
I've written a small utility function that I use for this. It takes a
MySQL result set as an argument, and returns an HTML table (string)
containing all the column headers and rows, formatted.

Here's the function:

function _mysql_result_all($result, $tableFeatures="") {
$table .= "<!--Debugging output for SQL query-->\n\n";
$table .= "<table $tableFeatures>\n\n";
$noFields = mysql_num_fields($result);
$table .= "<tr>\n";
for ($i = 0; $i < $noFields; $i++) {
$field = mysql_field_name($result, $i);
$table .= "\t<th>$field</th>\n";
}
while ($r = mysql_fetch_row($result)) {
$table .= "<tr>\n";
foreach ($r as $column) {
$table .= "\t<td>$column</td>\n";
}
$table .= "</tr>\n";
}
$table .= "</table>\n\n";
$table .= "<!--End debug from SQL query-->\n\n";
return $table;
}

You could use it like this, using your $ergebnis variable:

print _mysql_result_all($ergebnis);

That is, copy the function above into your script, and then use the
preceding line to output the result.

Enjoy...

--
//Marius

Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.