How can i display MySql results into a table?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
gegeor
Forum Newbie
Posts: 4
Joined: Wed Jul 09, 2003 6:09 am

How can i display MySql results into a table?

Post by gegeor »

Hello.:))

I am trying to display mysql results into a php page but i want them into a simple table...How can i do that..? I have tried many ways using html but nothing worked.....

Here is my code:

Code: Select all

<?php

$serv_cnx = mysql_connect("localhost" , "db_username", "db_password");
$db_cnx = mysql_select_db("db_name",$serv_cnx);
$query = mysql_query("select * from efit_results order by 'id'");

while ($row = mysql_fetch_array($query)) &#123;

echo " "
 . $row&#1111;"cell"]      # reult 1
 . "-->"
 ."<b>". $row&#1111;"value"]     ."</b>"   #result 2 
 . "<br/>"
 ."</p>"     ;

 &#125;
?>
Any help is appreciated
Thanks
:):):)
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

This should get you going in the right direction.
FYI: I did not test this code...

Code: Select all

<?php 

    $serv_cnx = mysql_connect("localhost" , "db_username", "db_password"); 
    $db_cnx = mysql_select_db("db_name",$serv_cnx); 
    $query = mysql_query("select * from efit_results order by 'id'"); 
    echo "<table>\n";
    echo "<tr>\n";
    while ($row = mysql_fetch_array($query)) 
    &#123; 
        echo "<td>\n"; 
        echo $row&#1111;"cell"]."# reult 1\n"; 
        echo "<b>". $row&#1111;"value"]."</b>#result 2\n"; 
        echo "</td>\n"; 
    &#125; 
?> 
</tr>
</table>
John M
gegeor
Forum Newbie
Posts: 4
Joined: Wed Jul 09, 2003 6:09 am

Post by gegeor »

I have managed to create the following:
(see webpage)
http://www.kastelliotis.net/sql4.php

The code is :

Code: Select all

<?php
    include("something.php");

    #if (!isset($mainfile)) &#123; include("mainfile.php"); &#125;

#$index = 1;

#include("header.php");


#OpenTable();

##################################


#include("header1.php");


while ($row = mysql_fetch_array($query)) &#123;


       $cell=$row&#1111;"cell"];
        $value=$row&#1111;"value"];
        $submit=$row&#1111;"date"];

print"<html>\n";
print"<head>\n";
print"</head>\n";
print"<body>\n";
print"<center>\n";
print"<table width="50%" cellspacing="1" cellpadding="1" border="1">\n";
print"<tr>\n";

print"<td>&nbsp;$cell</td>\n";
print"<td>&nbsp;<b>$value</td>\n";      #OMADES

print"  </tr>\n";
print"</table>\n";
print"</center>\n";
print"</body>\n";
print"</html>\n";




    &#125;

    echo " <p><a href='sql4.php?add=12'> Page #1</a></p>" ;

     echo     " <p><a href='sql4.php?add=24'> Page #2</a></p>" ;

      echo     " <p><a href='sql4.php?add=36'> Page #3</a></p>" ;


  

?>


  <?php

#CloseTable();

#include("footer.php");

#?>
But the table isnt very straight as you can see... Also is there a way to have background colors different at specific cells??

Any ideas.??

Thanks :roll: :roll:
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

This is some pretty basic html stuff here but look at this:

Code: Select all

<?php

    include("something.php"); 

    #if (!isset($mainfile)) 
    #{ 
    #    include("mainfile.php"); 
    #} 
    
    #$index = 1; 
	#include("header.php"); 
	#OpenTable(); 
	################################## 

	#include("header1.php"); 

	echo"<center>\n"; 
	echo"<table width="50%" cellspacing="1" cellpadding="1" border="1">\n"; 
	while ($row = mysql_fetch_array($query)) 
	{ 
        $cell=$row["cell"]; 
        $value=$row["value"]; 
        $submit=$row["date"]; 
		
		  echo "<tr>\n"; 
		  echo "<td width="50%" bgcolor="blue" align="right">$cell</td>\n"; 
		  echo "<td width="50%" bgcolor="red" align="right"><b>$value</td>\n";      #OMADES 
        echo "</tr>\n";
	} 
	echo "</table>\n"; 
	echo "</center>\n";  

   echo "<p><a href='sql4.php?add=12'>Page #1</a></p>" ; 
   echo "<p><a href='sql4.php?add=24'>Page #2</a></p>" ; 
   echo "<p><a href='sql4.php?add=36'>Page #3</a></p>" ; 

#CloseTable(); 
#include("footer.php"); 

?>
I did not test this but you should be able to get the idea. Pay attention to the way you code as well.

John M
gegeor
Forum Newbie
Posts: 4
Joined: Wed Jul 09, 2003 6:09 am

Post by gegeor »

Your code worked fine.:))) Thank you very much and forgive me about my code...I am new to all these....:)

Thanks again:)))
Post Reply