Php List box populated from MSsql query

I am trying to create a list box from a php query but I am getting an error what am I doing wrong?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Lab Support Service Request Page</title>
</head>
<body>
<?php
$dbc = mssql_connect ('cacapppro01.cup.com','nskinfo','webres');
$query = mssql_query("
SELECT     *
FROM         dbo.NSKSystemInfo
WHERE     (SystemState_ID = 1)
ORDER BY systemName");
?>
<?
echo "<select  size="10" multiple="yes" value="System_ID" display="SystemName" name=\"SYSTEM_ID\"></select>";
mssql_close($dbc);

?>
</body>
</html>
LVL 1
lhaluskaAsked:
Who is Participating?
 
Aamir SaeedConnect With a Mentor Commented:
i did post that code in MYSQL as i just wanted to give you an idea. now, i would like to let you know about your errors.

1; you need to use mssql_select_db() to select DB
2; you need to use mssql_fetch_array() to get data and then loop through it and display data
3; you can't use outer and inner single and double quotes at the same time for example,

echo "<select  size="10" multiple="yes" value="System_ID" display="SystemName" name=\"SYSTEM_ID\"></select>";

whether you 'll use outer single and inner double or vice versa and if you want to use the same then you can escape them by using \ before the inner quotes.

hope this helps
0
 
Aamir SaeedCommented:
i think you are trying to populate select box with the DB values?
0
 
lhaluskaAuthor Commented:
Yes, The table I want to show in the list is "SystemName" and the value I want is "System_ID".
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Aamir SaeedCommented:
just an idea


<?php
      include "canada_hotels/library/connection.php";
      
      $query  = "SELECT * FROM tblcategory";
      $result = mysql_query($query);
      ?>
      <select name='category'>
      <option></option>
      <?
      while($row = mysql_fetch_array($result, MYSQL_ASSOC))
      {
            echo "<option value='{$row['categoryID']}'";         
            echo ">{$row['categoryname']}</option>";      
      }
?>
      </select>
0
 
lhaluskaAuthor Commented:
So I have a couple of questions. Why is it that I need a "mssql_select_db" if I have the "$query" and the "$dbc" variable? The code is now working with your help from the example, But why am I getting a white space at the top of my list box?

<?php
$dbc = mssql_connect ('cacapppro01.cup.com','nskinfo','webres');
$query = mssql_query("
SELECT     *
FROM         dbo.NSKSystemInfo
WHERE     (SystemState_ID = 1)
ORDER BY systemName");

?>

<select  size="10" multiple="yes" value="System_ID" display="SystemName" name="SYSTEM">

      <option></option>
      <?
      while($row = mssql_fetch_array($query, MSSQL_ASSOC))
      {
            echo "<option value='{$row['System_ID']}'";        
            echo ">{$row['systemName']}</option>";      
      }
?>
      </select>


<?
mssql_close($dbc);

?>
0
 
lhaluskaAuthor Commented:
I answered my second question in the last post it was the option above the loop. Oh I have one more question how do I make it so users can not see the php code if they click view source?
0
 
Aamir SaeedCommented:
people can't see php code rather they can only see client side code for example HTML, CSS, JAVASCRIPT etc and PHP is server side language
0
 
lhaluskaAuthor Commented:
oh ok thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.