RyanW67 Posted October 3, 2007 Share Posted October 3, 2007 Hi All, I've searched the net for examples and have really hit a stump, I'm quite a newbie to MySQL so would really appreciate it if any of you guys could help... Basically I'm setting up a website which needs an populated drop down box made up from all fields in a specific column of a table in a mysql db.... Here's the code I've made up using various tutorials.... <?php $user = ""; $host = "" $password = "" $dbName = "" /* make connection to database */ mysql_connect($host, $user, $password) OR DIE( "Unable to connect to database"); mysql_select_db($dbName); //did you forget this line? $sql = "SELECT model FROM usedVehicles"; $query = mysql_query($sql) or die(mysql_error() . "<br>$sql"); //use the or die(...) part ONLY IN DEVELOPMENT ?> <form action="action" method="post"> <select name="option"> <?php while ($row = mysql_fetch_array($result)) { echo "<option value=\"" . $row['model'] . "\">" . $row['model'] . "</option>\n"; } ?> </select> <input type="submit"> </form> I've left out the connection details for obvious reasons... When I upload and try to test this, jus a blank drop down appears... there are definately fields in the column as I have tried the query on phpMyAdmin... All help is really appreciated. Ryan Quote Link to comment Share on other sites More sharing options...
haaglin Posted October 3, 2007 Share Posted October 3, 2007 The $query: $query = mysql_query($sql) or die(mysql_error() . "<br>$sql"); //use the or die(...) part ONLY IN DEVELOPMENT should be $result.. $result = mysql_query($sql) or die(mysql_error() . "<br>$sql"); //use the or die(...) part ONLY IN DEVELOPMENT that is what you use in the while loop: while ($row = mysql_fetch_array($result)) { Quote Link to comment Share on other sites More sharing options...
RyanW67 Posted October 3, 2007 Author Share Posted October 3, 2007 Thank you very much While we're on topic, what would you recommend being the easiest way to remove duplicates? (there will be duplicates in the column and I only want them to appear once) Quote Link to comment Share on other sites More sharing options...
haaglin Posted October 3, 2007 Share Posted October 3, 2007 Use DISTINCT.. $sql = "SELECT DISTINCT model FROM usedVehicles"; 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.