roeyhaim Posted November 2, 2010 Share Posted November 2, 2010 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: <?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: $data = $db->getall("users", "*"); foreach($data as $row){ echo $row['fullname']; echo "<BR>"; } can anybody help me with that? Quote Link to comment https://forums.phpfreaks.com/topic/217603-return-array-from-mysql/ Share on other sites More sharing options...
jcbones Posted November 2, 2010 Share Posted November 2, 2010 Try replacing the while statement with this one: while($row=mysql_fetch_assoc($result)){ $data[] = $row; //make sure $data is an array, so it captures all of the data, and not just the last row. } Quote Link to comment https://forums.phpfreaks.com/topic/217603-return-array-from-mysql/#findComment-1129692 Share on other sites More sharing options...
roeyhaim Posted November 2, 2010 Author Share Posted November 2, 2010 Thanks i didn't notice for that line Quote Link to comment https://forums.phpfreaks.com/topic/217603-return-array-from-mysql/#findComment-1129699 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.