Juarez Posted April 8, 2013 Share Posted April 8, 2013 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>' ); ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 8, 2013 Share Posted April 8, 2013 So, create the table row and put table headers in there. You haven't even tried... Quote Link to comment Share on other sites More sharing options...
Juarez Posted April 8, 2013 Author Share Posted April 8, 2013 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. Quote Link to comment Share on other sites More sharing options...
davidannis Posted April 8, 2013 Share Posted April 8, 2013 The query DESCRIBE my_table; should return column names from mysql Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 8, 2013 Share Posted April 8, 2013 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. Quote Link to comment Share on other sites More sharing options...
Juarez Posted April 9, 2013 Author Share Posted April 9, 2013 Ok, I'll look into using PDO and consider myself properly told off... thanks Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 9, 2013 Share Posted April 9, 2013 (edited) 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. Edited April 9, 2013 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Barand Posted April 9, 2013 Share Posted April 9, 2013 (edited) Basic method (pseudocode) row = fetch_assoc() colnames = array_keys(row) print column headings do { print column values } while row = fetch_assoc() Edited April 9, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
davidannis Posted April 9, 2013 Share Posted April 9, 2013 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 9, 2013 Share Posted April 9, 2013 So check if there's any rows first. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.