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?

7
  • 1
    Anything in your error logs? Commented Jul 25, 2014 at 17:12
  • Also, make sure there are no ", <, or > in any values of $DepName. Commented Jul 25, 2014 at 17:13
  • <option row? and you're using $rows <option row which btw, is invalid HTML. Commented Jul 25, 2014 at 17:17
  • @EdCottrell I found this in the error logs: Use of undefined constant i. Now I have a PHP plugin to shutdown before it fill my hard drive with errors being generated by an infinite loop. Commented Jul 25, 2014 at 17:18
  • @JoelTrauger Nevermind; Mariano's answer is correct. Commented Jul 25, 2014 at 17:19

3 Answers 3

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++)
Sign up to request clarification or add additional context in comments.

4 Comments

I was just about to comment on that lol @EdCottrell however am questioning <option row
@EdCottrell Edited my comment above. Seems like invalid HTML.
@Fred-ii- That's also true, but it seems the syntax error is what's killing the output entirely. An invalid attribute shouldn't break the output like this.
It filled my error log. 256MB and climbing...I hate infinite loops.
1

you missed a dollar sign:

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

should be

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

Comments

0

Discovered the error.

Instead of:

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

I needed to have:

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

2 Comments

"Discovered the error." So did a few others before posting this. You should accept the first answer given, mainly Mariano's
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.

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.