1

Im having some trouble running a MySQL function with PHP, its actually a series of 'DROP FUNCTION IF EXISTS' and 'CREATE FUNCTION' statements followed a query that uses the functions.

The whole thing runs in bare MySQL where i also use 'DELIMITER' statements (as is the way).

In PHP I've tried running the whole thing, function declarations and then query together as one big string which lead me to understand that i wouldn't need the 'DELIMITER' statements but when php gets to executing the query, it still cant access the functions.

Ive also tried using 'mysqli_multi_query()' in the same way and running each of the queries separately on the same link, but in the end i get the same errors.

I wont post my whole code, i have a feeling that Im doing something fundamentally wrong, my drop / function declarations look like :

DROP FUNCTION IF EXISTS PQTY

CREATE FUNCTION PQTY (v_product_id INT,v_p_qty INT, v_option_id INT, v_option_value_id INT) RETURNS INT
NOT DETERMINISTIC
BEGIN
    DECLARE qty INT;

    SELECT products_stock_quantity INTO qty
    FROM products_stock
    WHERE products_stock_attributes = CONCAT(v_option_id,'-',v_option_value_id)
    AND products_id = v_product_id;
    IF ISNULL(qty) THEN
        RETURN v_p_qty;
    ELSE
        RETURN qty;
    END IF;
END

and the main query :

SELECT PQTY(p.products_id,p.products_quantity, po.products_options_id, pov.products_options_values_id) AS 'qty'

FROM products p

LEFT JOIN products_description pd
ON p.products_id = pd.products_id

LEFT JOIN manufacturers m
ON m.manufacturers_id = p.manufacturers_id

LEFT JOIN products_attributes pa
ON p.products_id = pa.products_id

LEFT JOIN products_options po
ON po.products_options_id = pa.options_id

LEFT JOIN products_options_values pov
ON pov.products_options_values_id = pa.options_values_id

LEFT JOIN products_options_text pot
ON pot.products_options_text_id = po.products_options_id

Like i said, in PHP I've tried running each statement separately but on the same link and also as one big string and I've tried up till now using both the legacy mysql functions and mysqli functions (the procedural ones).

Any help would be greatly appreciated.

aarcarr
  • 186
  • 1
  • 14
  • Do you use a semicolon as delimiter between your SQL statements? – sqlab Sep 16 '14 at 09:40
  • When i tried the statements all together i tried with and without and got an error both ways. So the error would look like `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 'DROP FUNCTION IF EXISTS PMODEL;CREATE FUNCTION PMODEL (v_model VARCHAR(35), opt' at line 20` with and `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 'DROP FUNCTION IF EXISTS PMODELCREATE FUNCTION PMODEL (v_model VARCHAR(35), opti' at line 20` without – aarcarr Sep 16 '14 at 09:50
  • Do all statements work with **mysqli_query** as single statements? – sqlab Sep 16 '14 at 10:08
  • I only get an error from the main query so Im guessing they do... – aarcarr Sep 16 '14 at 10:12
  • What error? Why did you omit the **DELMITER** statement separating SQL and procedural part? Does the **DROP FUNCTION** statement work even when you get an error with 'mysqli_multi_query() ? – sqlab Sep 16 '14 at 10:17
  • What's the usual way to do this, am i going about it in the right way? – aarcarr Sep 16 '14 at 10:17
  • I took it out because it wasn't working in reference to http://stackoverflow.com/questions/5311141/how-to-execute-mysql-command-delimiter – aarcarr Sep 16 '14 at 10:19
  • So the above error only happens when i try to run all of the statements together, when i run them one at a time, only the main query gives an error. – aarcarr Sep 16 '14 at 10:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61332/discussion-between-sqlab-and-aarcarr). – sqlab Sep 16 '14 at 10:25

1 Answers1

0

You can not mix regular queries with create function statements in mysqli_multi_query, as they both require their own (semicolon) delimiter and you can not redefine the delimiter here. So you have to separate the statements.

sqlab
  • 6,412
  • 1
  • 14
  • 29
  • Thanks so much for your help, I didn't redefine the delimiter in the end but split each separate query up and ran it separately on the same connection (this time using the oo mysqli interface) with the last one being the query that provided results. You've put a lot of time into helping me, again, thank you very much!! – aarcarr Sep 16 '14 at 15:04