hisk Posted September 9, 2011 Share Posted September 9, 2011 Well, the title says it all, I do have a class named Database and one Users with multiple methods (code below). When i do try to use one user method (find_all) it returns 2 identical results from the database (and there is only one record in the DB). I tried to spot some mistake in the code but i couldn't find it, if you find something please tell... Database Class: <?php /** * Class Name: MySQLDatabase * * Most of the methods in this class are * database-neutral methods for code reusability. * * Author: hisk */ require_once("config.php"); class MySQLDatabase { private $connection; private $last_query; private $magic_quotes_active; private $real_escape_string; function __construct() { $this->open_connection(); $this->magic_quotes_active = get_magic_quotes_gpc(); $this->real_escape_string = function_exists("mysql_real_escape_string"); } public function open_connection() { $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS); if(!$this->connection) { die("Database connection failed: " . mysql_error()); } else { $db_select = mysql_select_db(DB_NAME, $this->connection); if (!$db_select) { die("Database selection failed: " . mysql_error()); } } } public function close_connection() { if(isset($this->connection)) { mysql_close($this->connection); unset($this->connection); } } public function query($sql) { $this->last_query = $sql; $result = mysql_query($sql, $this->connection); $this->confirm_query($result); return $result; } private function confirm_query($result) { // usable only in query() function (private) if(!$result) { echo "DB Query Failed: " . mysql_error(); // The last query that might got us the problem echo "Last Query Was: " . $this->last_query; } } // agnostic public function num_rows($result) { return mysql_num_rows($result); } public function affected_rows($result) { return mysql_affected_rows($result); } public function insert_id($result) { return mysql_insert_id($result); } public function fetch_array($result) { return mysql_fetch_array($result); } public function escape_value($value) { if($this->real_escape_string) { // undo any magic quote effect so mysql_real_esc_str can work if($this->magic_quotes_active) { $value = stripslashes($value);} $value = mysql_real_escape_string($value); } else { if (!$this->magic_quotes_active) { $value = addslashes($value); } } return $value; } } $database = new MySQLDatabase(); ?> User Class: <?php // smart to include the database class require_once("database.php"); class User { public static function find_by_id($id=0) { global $database; $result_set = self::find_by_sql("SELECT * FROM users WHERE id = {$id}"); $result = $database->fetch_array($result_set); return $result; } public static function find_all() { global $database; $result_set = self::find_by_sql("SELECT * FROM users"); return $result_set; } public static function find_by_sql($sql="") { global $database; $result_set = $database->query($sql); return $result_set; } } ?> TEST FILE: <?php require_once("../includes/database.php"); require_once("../includes/user.php"); $users = User::find_all(); $result = $database->fetch_array($users); foreach($result as $user) { echo "<pre>" . $user . "</pre>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246815-php-class-is-returning-two-identical-results-from-database/ Share on other sites More sharing options...
Pikachu2000 Posted September 9, 2011 Share Posted September 9, 2011 You're using mysql_fetch_array, which returns both an enumerated and an associative array simultaneously, unless either the MYSQL_ASSOC or MYSQL_NUM argument is passed to it. Default is MYSQL_BOTH. Then when you foreach() through the array, you're iterating over both the enumerated results and the associative results. If you also echo the key values, you should see both of them. To return only an associative array, use mysql_fetch_assoc, for only an enumerated array, mysql_fetch_row. Quote Link to comment https://forums.phpfreaks.com/topic/246815-php-class-is-returning-two-identical-results-from-database/#findComment-1267492 Share on other sites More sharing options...
hisk Posted September 9, 2011 Author Share Posted September 9, 2011 Pikachu2000 - Thanks a lot, it works well now. Well, that's why is good to read the whole php manual... Sorry for the small mistake, and for bothering you. Quote Link to comment https://forums.phpfreaks.com/topic/246815-php-class-is-returning-two-identical-results-from-database/#findComment-1267495 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.