mattm_14 Posted August 21, 2017 Share Posted August 21, 2017 Hi I am completely new to PHP and I am looking to populate a drop down box on my page with values from my database (list of venues). I have got the following code but nothing happens. I would really appreciate it if someone could assist? <select name="venue"> <?php $user = 'root'; $pass = 'admin'; $db = 'r_db'; $db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect"); $query = "SELECT Venue FROM venue_table"; $response = @mysqli_query($db,$query); $venue.=""; while($row = mysqli_fetch_array($response)){ $venue= "<option>" . $row['Venue']. "</option>", } echo $venue; ?> </select> Quote Link to comment Share on other sites More sharing options...
seany123 Posted August 21, 2017 Share Posted August 21, 2017 (edited) Hi I am completely new to PHP and I am looking to populate a drop down box on my page with values from my database (list of venues). I have got the following code but nothing happens. I would really appreciate it if someone could assist? <select name="venue"> <?php $user = 'root'; $pass = 'admin'; $db = 'r_db'; $db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect"); $query = "SELECT Venue FROM venue_table"; $response = @mysqli_query($db,$query); $venue.=""; while($row = mysqli_fetch_array($response)){ $venue= "<option>" . $row['Venue']. "</option>", } echo $venue; ?> </select> so first, you need to have a semicolon after assigning the variable, secondly you would want to append the values to the variable $venue rather than overwriting it on every run of the while loop... $venue = $venue . "<option>" . $row['Venue']. "</option>"; Edited August 21, 2017 by seany123 Quote Link to comment Share on other sites More sharing options...
mattm_14 Posted August 21, 2017 Author Share Posted August 21, 2017 (edited) thank you seany123, but I still don't get any values. I have adjusted to this: <select name="going"> <option value="0">Please Select</option> <?php $user = 'root'; $pass = 'admin'; $db = 'racing_database'; $db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect"); $query = "SELECT Venue FROM venue_table"; $response = @mysqli_query($db,$query); $venue=""; while($row = mysqli_fetch_array($response)){ $venue = $venue . "<option>" . $row['Venue']. "</option>"; } echo $venue; ?> </select> Edited August 21, 2017 by mattm_14 Quote Link to comment Share on other sites More sharing options...
seany123 Posted August 21, 2017 Share Posted August 21, 2017 thank you seany123, but I still don't get any values. I have adjusted to this: <select name="going"> <option value="0">Please Select</option> <?php $user = 'root'; $pass = 'admin'; $db = 'racing_database'; $db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect"); $query = "SELECT Venue FROM venue_table"; $response = @mysqli_query($db,$query); $venue=""; while($row = mysqli_fetch_array($response)){ $venue = $venue . "<option>" . $row['Venue']. "</option>"; } echo $venue; ?> </select> is the column called 'Venue' with a capital v in the sql table? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 21, 2017 Share Posted August 21, 2017 Turn on PHP error reporting and also check for mysql error messages. And stop using "@" to suppress error messages - you want to know what's wrong. Quote Link to comment Share on other sites More sharing options...
mattm_14 Posted August 21, 2017 Author Share Posted August 21, 2017 yes it is called "Venue" with a capital v. How do I turn on PHP error reporting? Quote Link to comment Share on other sites More sharing options...
Sepodati Posted August 21, 2017 Share Posted August 21, 2017 From someone's signature: PS - If you're posting here you should be using: error_reporting(E_ALL); ini_set('display_errors', '1'); at the top of ALL php code while you develop it! Quote Link to comment Share on other sites More sharing options...
Sepodati Posted August 21, 2017 Share Posted August 21, 2017 If your database connection fails, $db will be false. If your query fails, $response will be false. You're not checking for either of these. Printing mysqli_error($db) will show you why your query failed. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 21, 2017 Share Posted August 21, 2017 ^^^ it's even worse that that (thanks php.net.) when using OOP notation for the connection, an object is always returned and $db will always be an object (true) value. so, three things - 1) the or die(...) logic won't ever be triggered, 2) if you are using program logic to detect a connection error, you must use mysqli_connect_error() (or the ->connect_error property, assuming you have a php version where that works), and 3) if you can, switch to use the php PDO extension. it is much better implemented, simpler to use, and more constant than the php mysqli extension. avoid all the problems with handling connection and query errors by enabling exceptions, then simply let php catch the exception and use its error_reporting/display_errors/log_errors settings to control what happens with the actual error information. you can then remove (which currently isn't working anyways) or leave out any error checking logic in your code. to enable exceptions for the php mysqli extension, simply add the following line before the point where you are making the database connection - mysqli_report(MYSQLI_REPORT_ALL); 1 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.