Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 16th, 2005, 11:42 PM
Felix
Guest
 
Posts: n/a
Default Result of Mysql Query in a PHP table

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
  #2  
Old July 16th, 2005, 11:42 PM
Kevin Thorpe
Guest
 
Posts: n/a
Default Re: Result of Mysql Query in a PHP table

Felix wrote:[color=blue]
> 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");
>[/color]
echo "<table>";
echo "<tr><th>Referenznummer</th></tr>";[color=blue]
> $abfrage = "SELECT * FROM tabelle";
> $ergebnis = mysql_query($abfrage);
> while($row = mysql_fetch_object($ergebnis))
>
> {[/color]
echo "<tr><td>";[color=blue]
> echo $row->Referenznummer;[/color]
echo "</td></tr>";[color=blue]
> }[/color]
echo "</table>";[color=blue]
>
> ?>
>
>
> But I want that the result is in a Table. With the heading "Referenznummer".
> It should be like:[/color]

  #3  
Old July 16th, 2005, 11:42 PM
Marius Mathiesen
Guest
 
Posts: n/a
Default Re: Result of Mysql Query in a PHP table

Felix wrote:[color=blue]
> 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);[/color]
[color=blue]
> But I want that the result is in a Table. With the heading "Referenznummer".
> It should be like:
> ---------------
> Referenznummer:
> ---------------
> Result 1
> ---------------
> Result 2[/color]

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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.