Page 1 of 1

[RESOLVED]Write a function to get all records from a table

Posted: Thu Nov 06, 2008 4:58 am
by rasana
Hi All,

I'm working on a project where I have to write a function to get all records from a table which will return array of objects.
I've written a function to get a single record from table and working fine..but to get all records i'll have to write while loop..how would I store data in array and how would I display all data...?

Below is function to get a single record

Code: Select all

$sql = "select * from student where id = 1";
$getData = $db->getRecord($sql);
 
function getRecord($sql)
    {
        $result = mysql_query($sql) or die(mysql_error());
        $data_set = mysql_fetch_array($result);
            
    return $data_set;
    }  

Re: Write a function to get all records from a table

Posted: Thu Nov 06, 2008 5:15 am
by someberry

Code: Select all

$results = array();
while ($row = mysql_fetch_assoc($result)) {
    $results[] = $row;
}

Re: Write a function to get all records from a table

Posted: Thu Nov 06, 2008 5:33 am
by rasana
Thanks someberry!! its working fine.. :D