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

3
  • check, stackoverflow.com/questions/8022353/… Commented Jul 9, 2015 at 14:50
  • Only just a couple of steps and you are there. Look PHP_MYSQL for a mysql tutorial. And PHP ECHO for PHP echo and Tag_Option for a short HTML tutorial. Commented Jul 9, 2015 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 Commented Jul 9, 2015 at 14:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.