Problems with mysql_query and mysql_db_query
Posted: Fri Aug 19, 2005 9:36 am
I transfered my old DB stuff into a datalayer class shown below:
The _query is where my problem lies, I think.
I instantiate an inctance of DataLayer and connect as shown below:
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:
However, $Member = mysql_fetch_assoc(mysql_db_query($DB,$SQL)); gives me the desired array.
So where am I going wrong?
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;
}
}
?>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());
}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);So where am I going wrong?