drop down menu using MSSQL connection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    drop down menu using MSSQL connection

    I cannot select the distinct records but Im pretty sure im connected to the database. Please tell me what's wrong with this? and why I cannot see the query result?

    thanks!

    Code:
    echo "Status:"; 
    $res=mssql_query("SELECT distinct(contactmethod) FROM users with(nolock)"); 
    if(mssql_num_rows($res)==0){ 
    echo "there is no data in table.."; 
    } else { 
    echo '<select  name="status" id="contactmethod >'; 
    for($i=0;$i<mssql_num_rows($res);$i++) { 
    $row=mssql_fetch_assoc($res); 
    echo"<option value=\"$row[contactmethod]\" "; 
    if($contactmethod==$row[contactmethod]) 
    echo "selected"; 
    echo ">$row[contactmethod]</option>"; 
    } 
    echo "</select><font size='+2' color ='red'>*</font> "; 		
    }
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    What CAN you see.
    This for example will throw an error, not sure what level
    Code:
    echo"<option value=\"$row[contactmethod]\" ";
    An array element wrapped in quotes cannot be parsed without braces.
    But this is tidier
    Code:
    echo '<option value="',$row[contactmethod],'"';
    And where's your closing option tag?

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      Code:
      echo '<option value="' . $row['contactmethod'] . '"';
      Not surrounding the array element name in quotes means that the interpreter will have to take extra steps in identifying whether "contactmet hod" is a constant or a string. Also, using periods instead of commas in "echo" is considered the standard, since the period is out concatenation character for strings.

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        using periods instead of commas in "echo" is considered the standard
        Reference please.
        My understanding is commas are better performance wise

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #5
          I guess they are. Nvr really went to investigate. Originally, I was under the impression that echo only took 1 parameter, and as such we had to concatenate. Using commas meant that you were sending multiple parameters. I've seen it done a few times, but was under the impression that since echo was known to only take one parameter, it was performing the concatenation within the body. I tried it on a few test cases, and you're right. It is faster.

          Looks like I may be changing up part of the advice I give. :P

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            My understanding is commas are better performance wise
            the manual lists both ways, not making a recommendation, which should be preferred.

            Comment

            Working...