Reputation: 69
Here's a php class.
class WADB{
private $sDbHost;
private $sDbName;
private $sDbUser;
private $sDbPwd;
private $iNoOfRecords;
private $oQueryResult;
private $aSelectRecords;
private $aArrRec;
private $bInsertRecords;
private $iInsertRecId;
function __construct($sDbHost, $sDbName, $sDbUser, $sDbPwd){
// link to DB...
}
function selectRecords ($sSqlQuery){
unset($this->aSelectRecords);
$this->oQueryResult = mysql_query($sSqlQuery) or die(mysql_error());
$this->iNoOfRecords = mysql_num_rows($this->oQueryResult);
if ($this->iNoOfRecords > 0) {
while ($oRow = mysql_fetch_array($this->oQueryResult,MYSQL_ASSOC)) {
$this->aSelectRecords[] = $oRow;
}
mysql_free_result($this->oQueryResult);
}
$this->aArrRec = $this->aSelectRecords;
return array( 'data' => $this->aArrRec,
'record' => $this->iNoOfRecords);
}
}
And This is how I use it :
require_once('WADB.php');
$db = new WADB('{HOST}','{DB_NAME}','{User}','{Password');
$s1 = "SELECT * FROM {TableName} WHERE UserId='$_POST[UserId]';";
$d1 = $db->selectRecords($s1);
It didn't show the notice before as I used it, but now it shows
Notice: Undefined property: WADB::$aSelectRecords in {my_computer_url}\WADB.php on line 31
line 31 is :
$this->aArrRec = $this->aSelectRecords;
It didn't show before as I used it. I don't know why..?!
--
Sorry for my poor English, and hope you'll understand what I mean. ='(
Upvotes: 0
Views: 2390
Reputation: 2957
Inside your selectRecords
method, you have this: unset($this->aSelectRecords);
, which effectively unsets the aSelectRecords
data member. So, when you try to access the data later, it is not longer 'available'.
If what you are trying to do is to empty the array this variable holds, then don't use unset
, rather do this:
$this->aSelectRecords= array();
So you'd define the method as:
function selectRecords ($sSqlQuery){
$this->aSelectRecords = array(); //rather than `unset`
$this->oQueryResult = mysql_query($sSqlQuery) or die(mysql_error());
$this->iNoOfRecords = mysql_num_rows($this->oQueryResult);
if ($this->iNoOfRecords > 0) {
while ($oRow = mysql_fetch_array($this->oQueryResult,MYSQL_ASSOC)) {
$this->aSelectRecords[] = $oRow;
}
mysql_free_result($this->oQueryResult);
}
$this->aArrRec = $this->aSelectRecords;
return array( 'data' => $this->aArrRec,
'record' => $this->iNoOfRecords);
}
Upvotes: 1
Reputation: 194
Here you make unset of proprety unset($this->aSelectRecords);
After this move you have if()
and if if(false)
you try to get value of this proprety but his unset
and you get error.
Upvotes: 0