Hi guys,
i create my prepared statement as:
pg_prepare('stm_name', 'SELECT ...');
Today, i had a problem (calling twice a function for mistake) when declaring a prepared statement with the same name twice:
Warning: pg_prepare() [function.pg-prepare]: Query failed: ERROR: prepared statement "insert_av" already exists in xxx on line 221
So, as the question title, there is a way to check if a prepare statement with the same label already exists, and in case, overwrite it?
I know this error is come for a my mistake and will be solved simply declaring the prepared statements at the begin of my code, but im wondering if there is a solution to have more control over them.
EDIT:
After the Milen answer, is quite simply to check if the prepared statement is already in use, simply queryin the db for the table pg_prepared_statements:
try{
$qrParamExist = pg_query_params("SELECT name FROM pg_prepared_statements WHERE name = $1", array($prepared_statement_name));
if($qrParamExist){
if(pg_num_rows($qrParamExist) != 0){
echo 'parametized statement already created';
}else{
echo 'parametized statement not present';
}
}else{
throw new Exception('Unable to query the database.');
}
}catch(Exception $e){
echo $e->getMessage();
}
But, i dont think this is a good solution, becose i have to query the database everytime.
Ok, usually the prepared statements are declarated in the begin of the script and then just reutilized, but, i have a class nicely wired and i dont like to declare 10 prepared statement when i'll use just 3 of them.
So, i think i'll use a simple php array to keep track the statements i create, and then with isset() function check if it exist or need to be created:
try{
$prepare = pg_prepare('my_stmt_name', "SELECT ...");
if($prepare){
$this->rayPrepared['my_stmt_name'] = true;
}else{
throw new Exception('Prepared statement failed.');
}
}catch(Exception $e){
echo $e->getMessage();
}