Hi everyone, Until now i didnt use classes and functions for sql related actions and now i would like to start. I started out with this : class.php ---------------------------------------------------
class db { public function connect($database,$user,$password) { $db = mysql_connect("localhost",$user,$password) or die (mysql_error()); mysql_select_db($database); } static function fetch() { function getresults($query) { $resultArray = array(); $fetch = mysql_query($query) or die (mysql_error()); while ($row = mysql_fetch_assoc($fetch)){ $resultArray[] = $row; } return $resultArray; mysql_free_result($fetch); } } }
and index.php ----------------------------
<?php require('class.php'); $connection = new db(); $connection->connect("testdb","root","root"); $connection->fetch()->getresults("SELECT * FROM users"); foreach (getresults() as $user) { echo "<div>{$user['user_first_name']}</div>"; }
BUt it doesnt work. Actually the only way it works is :
$connection::fetch(); foreach (getresults("SELECT * FROM users") as $user) { echo "<div>{$user['user_first_name']}</div>"; }
Now my ultimate goal is : - I want to access db and get results anyware in my script to use : $connection->fetch("SQL QUERY"); and then foreach to echo the results. - I want to put more functions in fetch. For example if i want to fetch number of results to use $connection->fetch()->results(); Im sorry if i confused you! Basically i want to make everything tidy but im not familiar with the logic. I know basic class/function use. Thanks in advance