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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rasana
Forum Newbie
Posts: 9
Joined: Fri Aug 08, 2008 6:07 am

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

Post 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;
    }  
Last edited by rasana on Thu Nov 06, 2008 5:34 am, edited 1 time in total.
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

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

Post by someberry »

Code: Select all

$results = array();
while ($row = mysql_fetch_assoc($result)) {
    $results[] = $row;
}
rasana
Forum Newbie
Posts: 9
Joined: Fri Aug 08, 2008 6:07 am

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

Post by rasana »

Thanks someberry!! its working fine.. :D
Post Reply