Link to home
Start Free TrialLog in
Avatar of tankergoblin
tankergoblin

asked on

get record from class.php and display all record

Hi,

I use select clause to retrieve the values from the database.

i wrote my queries in a class. i name it as testClass.php

in my testClass.php i have this
<?php
class test{
....
while ($row = mysql_fetch_row($result)){
  echo "<tr>";
  foreach ($row as $field){
    echo "<td>".($field)."</td>";
  }
  echo "</tr>";
}
...
}

Open in new window


But above only show one row. how to get all the rows from database.

Also

instead create table in testClass.php i want to display the records in my result.php

where i write as below:
<html>
...
<?php
$newresult = new test();
echo $newresult-> $field;
?>
...
</html>

Open in new window


but above code doesn't work. It does not show loop of all the rows.

Thank you.
Avatar of sivagnanam chandrakanth
sivagnanam chandrakanth
Flag of India image

you have to do like this

class.php

class test{

      function testrecord(){
                  $con = mysql_connect("localhost","root","");
                  mysql_select_db("expert", $con);
                  $result = mysql_query("select * from box ");                             
                  while($res=mysql_fetch_object($result)){                  
                        $values[]= $res;
                        
                  }                  
        return $values;
      }
}

myresult.php

<?php
$newresult = new test();
print_r($newresult->testrecord());
?>
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There is a large red warning box in the online man page for this function.  It's time to move on to something newer and better.
http://php.net/manual/en/function.mysql-fetch-row.php

This book teaches how to use PDO.
http://www.sitepoint.com/books/phpmysql5/