I have this problem with my DB class. Whenever I do a query, mysql does not recognise the resource link identifier.
/**
* Inserts a new row in the db, returns insertId or false
*/
public function insert($table, $pairs)
{
$keys = array();
$values = array();
foreach($pairs as $k=>$v){
$keys[] = $k;
$values[] = $v;
};
$keys = implode(', ', $keys);
$values = implode("','", $values);
$q = "INSERT INTO {$table} ({$keys}) VALUES ('".$values."')";
if(false === ($this->res = mysql_query($q)) && $this->showErrors){
echo mysql_error($this->conn).'<br />'.$q;
};
return @mysql_insert_id($this->res);
}
This does not return the last insert id as it should. It used to give an error until I put the @ suppressor in there.
Secondly,
public function query($q)
{
if(false === ($this->res = mysql_query($q, $this->conn)) && $this->showErrors){
echo mysql_error().'<br />'.$q;
};
}
/**
* Number of rows that were affected by the last query
*/
public function affectedRows()
{
return mysql_affected_rows($this->res);
}
And using like this:
// update all subcats
$q = "UPDATE {$this->catTableName} SET parentKey = '{$newParentKey}' WHERE parentKey = '{$key}'";
$DB->query($q);
$numCats = $DB->affectedRows();
// update the nubmer of subcats for the new paretn
$q = "UPDATE {$this->catTableName} SET numSubCats = numSubCats + {$numCats} WHERE `key` = '{$newParentKey}'";
$DB->query($q);
. . . results in this error:
[INDENT]Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in C:\host\xampp\htdocs\studiojunkies\application\Antz\Db.php on line 79
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE key = 'root'' at line 1
UPDATE reviewsCats SET numSubCats = numSubCats + WHERE key = 'root'[/INDENT]
As you can see, the $numCats is empty because of the error. What I don't know is why mysql_affected_rows is not recognising $this->res after a query. Can anyone spot the problem?