return Array from mysql

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
roeyhaim
Forum Newbie
Posts: 1
Joined: Tue Nov 02, 2010 5:34 pm

return Array from mysql

Post by roeyhaim »

Hello.
I'm new to php and need a little help please.
I try to build a class that return an Array of all the table in my db.
and be able to get all the data by there col name (like username, password....)
but all i got is the last record from the db.
here is the class:

Code: Select all

<?php
    class db{
            function getall($table, $search){           
            $sql = "SELECT ". $search ." FROM " . $table;
            $result = mysql_query($sql) or die('Error, insert query failed' . mysql_error());
            while($row=mysql_fetch_assoc($result)){
                     $data = $row;
             }
            return $data;
}
and this is in the html page:

Code: Select all

$data = $db->getall("users", "*");
    foreach($data as $row){
        echo $row['fullname'];
        echo "<BR>";
}

can anybody help me with that?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: return Array from mysql

Post by yacahuma »

Code: Select all

    function getTablesMysql()
        {
            $sql = "show tables";
            $rs = $this->db->Execute($sql);
            $arr = array();
            if ($rs)
            {
                while (!$rs->EOF)
                {
                    $arr[] = $rs->fields[0];
                    $rs->MoveNext();
                }
            }
            return $arr;

        }
PS: this is using ADODB
Post Reply