0

Hello I'm querying a database of names by the first letter of the last name. However when I excute the query and print the results, the first name prints over and over again when in reality theres more than one name to print out. here is what I have so far. the data passed in is a letter in which it supppose to gather all the last names with that beginning letter. what I'm I doing wrong that can cause this infinite loop?

function displayprofs()
{
print"<div>";
print "<p><a href = '$_SERVER[PHP_SELF]'>return to start</a>\n";
$abc=($_POST['abc']);
print"$abc";
$db = adodbConnect();
$query="Select * FROM Category WHERE Description LIKE '$abc%'";
$result=$db->Execute($query);
$row=$result->FetchRow();
while($row)
    {

      $name= $row['Description'];
      print "<form method='post' enctype='multipart/form-data' action='$_SERVER[PHP_SELF]'>\n";
      print"<input type='hidden' name='profy' value='$name'>";
      print"<p>$name<input type='submit' name='add' value ='Submit'/></p>\n";                                                   //submit button
      print"</form>\n";
      //break;
    } 
print"</div>";  
}
2
  • sorry it was a autocorrect error Commented Dec 15, 2012 at 10:51
  • while($row=$result->FetchRow()) instead of while($row) and remove $row=$result->FetchRow() Commented Dec 15, 2012 at 10:52

4 Answers 4

4

You fetch $row once and then start while loop with its condition always true. Missing $row=$result->FetchRow(); inside the while block.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi I replaced it and it still give me the infinite loop, it crashes my browser too.
4

Replace while($row) with while($row=$result->FetchRow())

and remove $row=$result->FetchRow(); you've written before starting while

Comments

0

Since you have while($row) you will iterate over the same row over and over again. Change to while($row=$result->FetchRow()) instead and remove the previous fetch or keep as is and put $row=$result->FetchRow(); in the end of the while block.

Solution 1:

    while($row=$result->FetchRow())
    {

      $name= $row['Description'];
      print "<form method='post' enctype='multipart/form-data' action='$_SERVER[PHP_SELF]'>\n";
      print"<input type='hidden' name='profy' value='$name'>";
      print"<p>$name<input type='submit' name='add' value ='Submit'/></p>\n";                                                   //submit button
      print"</form>\n";
    }

Solution 2:

    $row=$result->FetchRow();
    while($row)
    {
      $name= $row['Description'];
      print "<form method='post' enctype='multipart/form-data' action='$_SERVER[PHP_SELF]'>\n";
      print"<input type='hidden' name='profy' value='$name'>";
      print"<p>$name<input type='submit' name='add' value ='Submit'/></p>\n";                                                      //submit button
      print"</form>\n";
      $row=$result->FetchRow();
    } 

Comments

0

By now, you know the while($row) bit is probably a mistake. Just to avoid you having to post a second question in a minute: This isn't the best of ideas:

print "<form method='post' enctype='multipart/form-data' action='$_SERVER[PHP_SELF]'>\n";

Perhaps consider writing this as either:

print "<form method='post' enctype='multipart/form-data' action='{$_SERVER['PHP_SELF']}'>\n";

or:

print "<form method='post' enctype='multipart/form-data' action='".$_SERVER[PHP_SELF]."'>\n";

Comments

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.