Page 1 of 1

Problems with mysql_query and mysql_db_query

Posted: Fri Aug 19, 2005 9:36 am
by Synchronium
I transfered my old DB stuff into a datalayer class shown below:

Code: Select all

<?

class DataLayer {

	//
	// Class Properties
	//
	var $link;
	var $errors = array();
	var $debug = false;


	//
	// Empty Constructor
	//
	function DataLayer() {

	}


	//
	// Connects To Database
	// Returns true On Successful Conenction
	// Returns false If Error
	//
	function connect( $host, $name, $pass, $db ) {
		$link = mysql_connect( $host, $name, $pass);
		if ( ! $link ) {
			$this->SetError("Couldn't Connect To Database Server<br />");
			return false;
		}
		$this->link = $link;
		if ( ! mysql_select_db( $db, $this->link ) ) {
			$this->SetError("Couldn't Select Database: $db<br />");
			return false;
		}
		return true;
	}

	
	function GetError() {
		return $this->errors[count($this->errors)-1];
	}


	function SetError( $str ) {
		array_push( $this->errors, $str );
	}


	function _query( $query ) {
		if ( ! $this->link ) {
			$this->SetError("No Active DB Connection<br />");
			return false;
		}
		$result = mysql_query( $query, $this->link );
		if ( ! $result ) {
			$this->SetError("Error: " . mysql_error() . "<br />Query: " . $query );
		}
		return $result;		
	}


	function SetQuery( $query ) {
		if ( ! $result = $this->_query( $query ) ) {
			return false;
		}
		return mysql_affected_rows( $this->link );
	}


	function GetQuery( $query ) {
		if ( ! $result = $this->_query( $query ) ) {
			return false;
		}
		$ret = array();
		while( $row = mysql_fetch_assoc( $result ) ) {
			$ret[] = $row;
		}
		return $ret;
	}
}

?>
The _query is where my problem lies, I think.

I instantiate an inctance of DataLayer and connect as shown below:

Code: Select all

$dl = new DataLayer();
if ( ! $dl->connect( $host, $name, $pass, $db ) ) {
	echo($dl->GetError());
}
Which gives me no problems, and for the most part I can execute queries using my methods fine. But one one page of the site, the query fails. It's not the SQL, it's the fact that $dl->_query uses "mysql_query" where the old code used "mysql_db_query". Now, I called this same function to execute a query on the same page using the same database, but the following chink of code doesn't work regardless:

Code: Select all

// Run SQL - Select MemberID
$SQL = "SELECT * FROM `kgb_members` WHERE `kgb_members`.`ID` = $MemberID";
$Member = $dl->GetQuery($SQL) or die ($dl->GetError);
However, $Member = mysql_fetch_assoc(mysql_db_query($DB,$SQL)); gives me the desired array.

So where am I going wrong?

Posted: Fri Aug 19, 2005 9:43 am
by feyd
if you're sure that $DB and $db are the same, you could try:

Code: Select all

$SQL = "SELECT * FROM `{$DB}`.`kgb_members` WHERE `kgb_members`.`ID` = $MemberID";
$Member = $dl->GetQuery($SQL) or die ($dl->GetError);

Posted: Fri Aug 19, 2005 9:49 am
by Synchronium
Sorry, I meant $DB to be $db in that last bit of code.

Like I said though, I selected the DB right at the beginning of the code (using $dl->connect();) and haven't changed anywhere.