PHP: create and run PostGres Procedure (BEGIN ... END) with binding parameters - php

I am using Postgres with PHP, I have two dependent queries as below:
INSERT INTO tab2
SELECT * FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL;
DELETE FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL ;
I want these both to run or none of them. So I created below code block:
DO $$
BEGIN
INSERT INTO tab2
SELECT * FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL ;
DELETE FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL ;
END$$
But this is not working and giving below error:
ERROR: bind message supplies 1 parameters, but prepared statement "" requires 0
PHP Code:
$result = pg_query_params($pg_con_write,$query_PG,array($Param1));
I think I can't use Bind Parameters in this way... So please suggest the best possible way to do so...

I think $$ is the issue, you should rewrite your query like:
BEGIN;
INSERT INTO tab2
SELECT * FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL;
DELETE FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL;
COMMIT;

Related

MySql stored procedure validation

I need a stored procedure to perform an action according to a condition that is saved in a PHP variable. This stored procedure would run one of possible 2 insert operations:
if $var = 1 then insert 1 else insert 2
Is this possible or will I have to do 2 stored procedures and do the validation in the php script?
Example:
DELIMITER $$
CREATE PROCEDURE example_sp
(in val1 char(100), in val2 char(50), in val3 char(10), in val4 char(50))
BEGIN
IF($var = 1)
THEN
INSERT INTO table1 (val1, val2) VALUES (val1, val2);
ELSE
INSERT INTO table2 (val1, val2) VALUES (val1, val2);
END IF;
END $$
DELIMITER ;
No you need only one stored procedure.
but for this you need dynamic sql
DELIMITER $$
CREATE PROCEDURE example_sp
(IN var int, in val1 char(100), in val2 char(50), in val3 char(10), in val4 char(50))
BEGIN
IF(var = 1)
THEN
SET #sql := CONCAT("INSERT INTO table1 (",val1,",", val2,") VALUES (?,?);");
SET #a := val1;
SET #b := val2;
ELSE
SET #sql := CONCAT("INSERT INTO table2 (",val1,",", val2,") VALUES (?,?);");
SET #a := val1;
SET #b := val2;
END IF;
PREPARE stmt FROM #sql;
EXECUTE stmt USING #a,#b;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
And in PHP
$stmt=$db->prepare("CALL example_sp(?,?,?,?,?)");
$stmt->bind_param('issss',$var,$val1,$val2,$val3,$val4);
$stmt->execute();

MySQL: update different columns with separate conditions

Please how can I update different columns base on different and specific conditions. For example:
UPDATE table
SET col1 = val1 WHERE col1 > 2
SET col2 = val2 WHERE col2 > 1
Is is possible to write an SQL UPDATE statement like this
Where different columns will be updated base on separate conditions?
Use case:
UPDATE table
SET col1 = (CASE WHEN col1 > 2 THEN val1 ELSE col1 END),
col2 = (CASE WHEN col2 > 1 THEN val2 ELSE col2 END);
You can also add WHERE col1 > 2 or col2 > 1 so MySQL does not attempt to update all rows.

select box typeable and search the data in the database contents

how to make a select box typeable, in the sense how can i type inside a select box , the value need not be in any of the options. The second part is that i need to search for this data in my entire database and retrieve the results. Is there any efficient way rather than this:
SELECT * FROM db1 WHERE id = ".$myVAR." OR col1= ".$myVAR." OR col2 OR col3 = ".$myVAR."
SELECT * FROM db2 WHERE id = ".$myVAR." OR col1= ".$myVAR." OR col2 OR col3 = ".$myVAR."
SELECT * FROM db3 WHERE id = ".$myVAR." OR col1= ".$myVAR." OR col2 OR col3 = ".$myVAR."
#Syamili V, Please check below solution for your problem :
There is a good example of precisely this on the jQueryUI demo page for auto-complete functionality. It actually uses a select element from data.
You could easily modify this to use a remote data source (Your database data result).

MySQLi: Best practice to select all where [column] = # and process

I am looking for the best practice for processing this query. I just want to make sure I am doing it right.
Query: Need to select all from database table where [column] = # and copy results to another table.
DELETE FROM table WHERE [column] = #
INSERT INTO table2 (col1, col2, col3)
(SELECT col1, col2, col3 FROM table WHERE [column] = #)
CREATE TABLE newtable LIKE originaltable; #This creates a new table just like original table
INSERT INTO newtable (SELECT * FROM originaltable WHERE column = '#'); #this moves selected records from original table to new created table

Which column matched?

How do I know which column matched if I have two LIKE clauses for two different columns and eventually one of the two columns contains the needle.
you could do something like:
SELECT 1 AS `match`, * FROM `table` WHERE col1 LIKE 'needle%'
UNION
SELECT 2 AS `match`, * FROM `table` WHERE col2 LIKE 'needle%'
and use match to know
Note: this could probably be written with a IF()
SELECT
IF(col1 LIKE 'needle%' AND col2 LIKE 'needle%', 0, IF(col1 LIKE 'needle%', 1, 2)
AS match,
*
FROM
table
WHERE
col1 LIKE 'needle%'
OR
col2 LIKE 'needle%'
match teklls you which colums hit:
0 = both
1 = match on col1
2 = match on col2
You can use your PHP section to check which one matched.
if (stristr($result['col1'], $queryvar) === true) // col1 matched

Categories