Jump to content

PHP Class is returning two identical results from database


hisk

Recommended Posts

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>";
   }

        ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.