0

I need some help with populating a dropdown list, I have been searching around to find solution with some trial and errors but I must admit, I never touch PHP. I have the following code but it does not display anything, I assume my problem is with the connection, so my question is how to build such a connection:

<?php
    $sql = mysql_query("SELECT ProvinceNameFR FROM Province");
    while ($row1 = mysql_fetch_array($sql)) {
        echo "<option value='".$row1['value']."'>".$row1['value']."
</option>";    
    }  
?>

Where do I go from that? I am involved in a project that someone else developed. Thanks

Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
PhpNewbie
  • 11
  • 2
  • check, http://stackoverflow.com/questions/8022353/how-to-populate-html-dropdown-list-with-values-from-database –  Jul 09 '15 at 14:50
  • Only just a couple of steps and you are there. Look [PHP_MYSQL](http://php.net/manual/en/book.mysql.php) for a mysql tutorial. And [PHP ECHO](http://php.net/manual/en/function.echo.php) for PHP echo and [Tag_Option](http://www.w3schools.com/tags/tag_option.asp) for a short HTML tutorial. – B001ᛦ Jul 09 '15 at 14:51
  • I have found out that this project is using Lavarel. also found at the top of the code the following: $record_set = sqlsrv_query($GLOBALS["dbcon"], $tsql, array(&$resto_id_value)); which is includedd in a if statement only, the more I look at this code, the more it does not make sense – PhpNewbie Jul 09 '15 at 14:56

2 Answers2

0

You've got some work ahead of you. First, use MySQLi or PDO instead of MySQL. MySQL is deprecated and will eventually be removed in future versions. http://php.net/manual/en/book.mysqli.php

Some obvious mistakes. You're query is SELECT ProvinceNameFR FROM Province but you're trying to get $row1['value'] when you should be using $row1['ProvinceNameFR'] because that's the column you selected.

You also need to wrap your <option>'s with the actual <select> tag. You may already be doing that, I just don't see it here.

so

<?php
$sql = mysql_query("SELECT ProvinceNameFR FROM Province");
echo "<select>";
while ($row1 = mysql_fetch_array($sql)) {
echo "<option value='".$row1['ProvinceNameFR']."'>".$row1['ProvinceNameFR']."
</option>";    
}  
echo "</select>";
?>

This is assuming everything is working database side. If not try adding var_dump(mysql_error()); at the end. That should print out the latest error and help you find out what's going wrong.

Squeegy
  • 869
  • 9
  • 20
0

I would recommend using PDO to connect to a MySQL db as the older mysql_query, mysql_connect systems are depreciated.

A very basic way to get started would be:

    $db = new PDO('mysql:host=HOST;dbname=DATABASENAME', USERNAME, PASSWORD);
    $query = "SELECT * FROM Province";

    $stmt = $db->query($query);
    $results['data'] = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo '<select>';
        foreach ($results['data'] as $row){
            echo "<option value='".$row['ProvinceId']."'>".$row['ProvinceNameFR']."</option>";    
        }
    echo '</select>';

You also need the <select> html wrappers around the options. And your field names need to be set to match the what is in the table. I have also assumed that there should be an ID in the value field.