Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
this is my function..

PHP
public function _GetResults($query)
        {
            $array = array();
            $result = mysql_query($query);
            if(!$result)
            {
                die('Invalid query: ' .mysql_error());
            }
            $array=mysql_fetch_assoc($result);
            return $array;
        }


this is my code to get the results, the results will display but it will not stop looping

XML
<?php

            echo "<table>";
            echo "<tr><th>First Name</th><th>Last Name</th></tr>";

            $Connection->_CreateConnections();
            $result = $Connection->_GetResults("select fname,lname from tblNames");

            while($result)
            {
                echo "<tr>";
                echo "<td>".$result["fname"]."</td>";
                echo "<td>".$result["lname"]."</td>";
                echo "</tr>";
            }

            echo "</table>";

            $Connection->_CloseConnections();
        ?>


the output goes like this

FIRSTNAME LASTNAME
john doe
john doe
john doe
Posted

I'm not a PHP expert, but I suspect you might have more luck if you used a foreach loop:
PHP
foreach ($result as $row)
{
    echo "<tr>";
    echo "<td>"$row["fname"]."</td>";
    echo "<td>"$row["lname"]."</td>";
    echo "</tr>";
}
 
Share this answer
 
Comments
Mohibur Rashid 24-Apr-12 4:46am    
actually his mistake is understanding with the required function related with mysql
it was suppose to do that.

look at this lines
PHP
            $result = $Connection->_GetResults("select fname,lname from tblNames");
//what does result contain? result contain an array, to d 
        ?>


to do it properly rewrite _GetResults as below:
PHP
public function _GetResults($query)
        {
            $array = array();
            $result = mysql_query($query);
            if(!$result)
            {
                die('Invalid query: ' .mysql_error());
            }
            return $result;
        }


also change the while loop as below:
PHP
while(($row=mysql_fetch_assoc($result)) //if row return null then the loop will terminate
  {
               echo "<tr>";
               echo "<td>".$row["fname"]."</td>";
               echo "<td>".$row["lname"]."</td>";
               echo "</tr>";
           }
mysql_free_result($result);//never forget this line
 
Share this answer
 
v2
Comments
Mohibur Rashid 24-Apr-12 4:56am    
unwanted tag has been added automatically

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900