I am trying to insert into my table, but only if the information doesn't exist already.
This works:
$sql = //"if not exists (select `id` from friends where `id` = :id) " . //ERROR!
"insert into `friends` (`id`, `fb_id`, `name`, `id2`, `fb_id2`, `name2`) " .
"values (:id, :fb_id, :name, :id2, :fb_id2, :name2)";
try
{
$stmt = $this->db->prepare($sql);
if (!$stmt)
{
$errorcode = (string)
$stmt->errorCode();
trigger_error("mysql errorcode : " . $errorcode, E_USER_ERROR);
$data = array( "note" => "mysql error code: " . $errorcode );
return json_encode( $data );
}
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':fb_id', $fb_id, PDO::PARAM_INT);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':id2', $id2, PDO::PARAM_INT);
$stmt->bindParam(':fb_id2', $fb_id2, PDO::PARAM_INT);
$stmt->bindParam(':name2', $name2, PDO::PARAM_STR);
$result = $stmt->execute();
$stmt->closeCursor();
}
catch (Exception $e)
{
die ($e->getMessage() );
}
I tried if not exists ( select id ...
etc as commeted out in the first line, but that didn't work. It returned error code 00000
with (string) $stmt->errorCode();
, I googled it and it means: to many mysql connections
, this can't be the problem as other queries work fine.
How do I insert into the table when not exist and how do I get mysql error information?