Jump to content

[SOLVED] Print Database in table


denoteone

Recommended Posts

I am try to make a dynamic script that will print  data from a table into a nice neat HTML table.

 

The only problem is I am only given the table name and one search able parameter and the value of that parameter. So I do not know how many columns there are since It varies.  below will maybe help you visualize what I am trying to do.

 

url=    http://www.mysite.com?table=visitors&param1=name&var1=mike

$table = $_GET['table'];
$param1 = $_GET['param1'];
$var1 = $_GET['var1'];

//database connect code

$query  = "SELECT * FROM '$table' WHERE '$param1'  = '$var1'";
$result = mysql_query($query)or die (mysql_error());	

now I want to print the data that is returned so I have to first find out how many total columns there are and there names. would I use two foreach loops with in earch other? one for each line returned in the query? and then the value of the columns within that line?  can anyone help me with this?  


Link to comment
Share on other sites

You can use mysql_fetch_row and then loop through that. :D

 

Or before all that, run the MySQL command -

DESC $table

. That would tell you the fields. Loop through that to grab all the names of the columns. Display them on the screen and you'll know exactly what you have to work with. Then modify the SQL you have up there. :)

Link to comment
Share on other sites

You can use mysql_fetch_row() and then loop through that.

 

Thanks Ken2k7 but I am still a little unclear. i checked out php.net/mysql_fetch_row()  and have a question.

 

Would it be possible to do something like...

$query  = "SELECT * FROM '$table' WHERE '$param1'  = '$var1'";
$result = mysql_query($query)or die (mysql_error());  

foreach ($result as $line)
{
         echo "<tr>";
       foreach( $line as $values) //loop through table columns and print them in cells but I dont know how many times to loop in this one.
		{
                        echo "<td> '{$result[i]}' </td>";
		}
         echo "</tr>";
}
echo "</table>";

Link to comment
Share on other sites

This is how i got it done incase anyone has this issue in the future.

 

$table = $_GET['table'];
$param1 = $_GET['param1'];
$var1 = $_GET['var1'];
$param2 = $_GET['param2'];
$var2 = $_GET['var2'];
$border = $_GET['border'];
$color = $_GET['color'];


	require_once('visitors_connections.php');
	mysql_select_db($database_visitors, $visitors);



						$query  = "SELECT * FROM $table WHERE $param1  = '$var1' and $param2 = '$var2'";
						$result = mysql_query($query)or die (mysql_error());
						echo "<table width=100% style='font:12px arial; color:{$color}'>";
     							 while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                              				echo "<tr>";
                                    				 $i = "0";
                                    				foreach($row as $value){

                                   				  echo "<td>{$value}</td>";

                                     				 $i = $i + "1"; 

                                     				 }


                              				echo "</tr>";
                        					 }

					 echo "</table>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.