John Davis John Davis - 1 year ago 41
PHP Question

PHP SQLSRV Dropdown list error

I am trying to populate dropdown list from SQL Query through sqlsrv
but it is not working. What am i doing wrong?


$serverName1 = "kk12334";
$connectionInfo1 = array( "Database"=>"Fruits");
$conn1 = sqlsrv_connect( $serverName1, $connectionInfo1);
$sql1="SELECT [Name] as CName,[BName] as BName from Fruits";
$stmt1 = sqlsrv_query( $conn1, $sql1 );

while ($data=sqlsrv_fetch_array($stmt1, SQLSRV_FETCH_ASSOC))


echo "<option value=";
echo $data['CName'].", ".$data['BName'].;
echo "<br />";
echo $data['CName'].", ".$data['BName'];
echo "</option>";



Answer Source

You cannot place a <BR> inside an <option> tag.

Try something like:

while ($data=sqlsrv_fetch_array($stmt1, SQLSRV_FETCH_ASSOC))
{ $opt="$data[CName], $data[BName]";
   echo "<option value='$opt'>$opt</option>\n";

And since you want a blank between your two variable values you need to enclose everything in ' to make sure both parts are assigned to value=.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download