Jump to content

display column names from database in returned values table


Juarez

Recommended Posts

Hi.

I have a html seach form and a php page. basically a user can enter the  column name selections in the search boxes  and select the relevent columns from the database. the data is returned dynamically to a table with mysql_fetch_array but I would like the column names from the database displayed at the top of returned table.

thanks

 

 

 

 

 

 


//index.html 


<html>
	<head>
		<title>Search the Database</title>
	</head>

	<body>

	<form action="search_column.php" method="post">
	
<br />
	Search ID1: <input type="text" name="column1" /><br />
	Search ID2: <input type="text" name="column2" /><br />
	Search ID3: <input type="text" name="column3" /><br />
	<br />
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>



	</body>
</html>




//search_column.php



<?php


require_once('includes/connection.inc.php');


$column1 = $_POST['column1'];
$column2 = $_POST['column2'];
$column3 = $_POST['column3'];



$qry=mysql_query("SELECT * FROM mytable", $con);


echo "<table border='3'  width = '150'>";
echo "<tr>   </tr>";


/* Fetching the data  */
while($row=mysql_fetch_array($qry))
{
echo "<tr>";

echo "<td>".$row{$column1}. "<br></td>";
echo "<td>".$row{$column2}. "<br></td>";
echo "<td>".$row{$column3}. "<br></td>";



echo "</tr>";
}
echo "</table>";


 print( '<a href=index.html>Go back to search</a>' );
?>









  On 4/8/2013 at 3:07 PM, Juarez said:

Ok,  sorry I forgot mention if the user is entering only the key number from the array and this is being posted  rather than the column name how you you then return the column name from the database.

From WHAT array?

 

When you select data from the database, you can always specify that you want an associative array. Use mysql_fetch_assoc (Really, just switch to PDO) If you're actually selecting columns by the number, then stop doing that. Not only is it harder to read and understand, it's preventing you from reaching your goal.

your code should be doing everything dynamically using the column names.

 

the post by davidannis would let you get all the column names. you would use those column names or a subset of them in your form and the actual column names would be submitted to use in the display logic.

 


since any column name could be submitted by altering the form data, you should validate the submitted column names before you use them in case you don't want to permit the display of some of the information, such as displaying a "password" column or displaying personal data to visitors not authorized to see that type of information.

Barand,

 

I do something similar to the pseudocode that you propose in a lot of my programs and I keep meaning to replace it with 

DESCRIBE my_table;

to get the column names.

The problem with the pseudocode is that

row = fetch_assoc()
colnames = array_keys(row)

fails if the select returns zero rows.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.