0

I have a PHP script that queries a database and fills out some <option> tags inside a <select> tag. See code below:

$stmt = sqlsrv_query($dbc, $tsql, $params, $dbcOptions);
if($stmt === false) {
        die ( print_r (sqlsrv_errors(), true));
}
$rows = sqlsrv_num_rows($stmt);
#echo $rows.'rows';
echo '<!--'.$rows.'-->';
echo '<select onchange="getNamesByDep(this.value)">';
echo '<option value="">Select a Department. . .</option>';

for ($i = 1; i <= $rows; $i++)
{
        if(sqlsrv_fetch($stmt) !== false)
        {
                $DepName = sqlsrv_get_field($stmt,0);
                echo '    <option row="'.$i.'" value="'.$DepName.'">'.$DepName.'</option>'."\r\n";
        }
}
echo '</select>';
echo 'Debugging';

It never echos </select> or Debugging to the HTML of the page.

I know that I configured $dbc, $tsql, $params, and $dbcOptions correctly because I am getting the desired results from my query.

Do I have a syntax error that my web server (WIMPServer) isn't catching?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Joel Trauger
  • 720
  • 1
  • 4
  • 24

3 Answers3

3

I think I found it:

for ($i = 1; i <= $rows; $i++)

You forgot the dollar sign before the second i. It should be like this:

for ($i = 1; $i <= $rows; $i++)
Mariano D'Ascanio
  • 1,202
  • 2
  • 16
  • 17
1

you missed a dollar sign:

for ($i = 1; i <= $rows; $i++)

should be

for ($i = 1; $i <= $rows; $i++)
Asterisk
  • 96
  • 2
0

Discovered the error.

Instead of:

for ($i = 1; i <= $rows; $i++)
{
        etc
}

I needed to have:

for ($i = 1; $i <= $rows; $i++)
{
        etc
}
Joel Trauger
  • 720
  • 1
  • 4
  • 24
  • 2
    *"Discovered the error."* So did a few others before posting this. You should accept the first answer given, mainly Mariano's – Funk Forty Niner Jul 25 '14 at 17:23
  • I accepted it. I discovered the error after Ed asked me to check the error logs. I thought I was posting an answer before anyone else. – Joel Trauger Jul 25 '14 at 17:30