4

My query is somewhat like this :

CREATE FUNCTION func ... ;
SELECT * FROM  ....;

I'm using php's PDO which doesn't allow more than 1 statements in a $pdo->query() , so instead I decided that I'd split the query into two, so I execute the CREATE FUNCTION query using $pdo->exec() and then use $pdo->query() on the select statement.

However, I get the error on execution of select statement that FUNCTION database.func does not exist. How do I solve this? when I run it in phpmyadmin as a single query it works fine

Edit : PHP code :

class MyClass {
function connectPDO($user,$pass,$chartset="utf8"){
    //simple function for making a new PDO object and mysql connection
    $dbname = "my_db";
    try {
        //creating new pdo
        $pdo = new PDO('mysql:host=localhost;dbname='.$dbname, $user,$pass);
        //attributes set to throw errors and exceptions which can be caught, can be changed
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        //fetch associative arrays
        $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
        //default chartset
        $pdo->exec('SET NAMES "'.$chartset.'"');
      } catch (PDOException $e) {
        //on some error
        $output = "Ay caramba! there's something wrong here!.<br>";
        echo $output.$e->getMessage();
        exit();
    }

    $this->db = $pdo;
}
}

$object = new MyClass();
$object->connectPDO("user","pass");
$sqlPreliminary = "
        DROP FUNCTION IF EXISTS myFunc;
        DELIMITER //

        CREATE FUNCTION myFunc (id INT)
            RETURNS DECIMAL

            BEGIN
            RETURN id + 1;
            END //
        DELIMITER ;

";

$sqlFinal = "
    SELECT id, myFunc(id) AS plusOne  FROM table;
";
$object->db->exec($sqlPreliminary);
var_dump($object->db->query($sqlFinal)->fetchAll(PDO::FETCH_ASSOC));
Peeyush Kushwaha
  • 3,453
  • 8
  • 35
  • 69

3 Answers3

5

Create a user defined function using PDO simply works by exec, no delimiter is required. Here $db is PDO instance.

$db->exec('CREATE FUNCTION ConvertHTMLToText(str LONGTEXT CHARSET utf8)
           RETURNS LONGTEXT CHARSET utf8
           BEGIN
           DECLARE start, end INT DEFAULT 1;
           LOOP
           SET start = LOCATE("<", str, start);
           IF (!start) THEN RETURN str; END IF;
           SET end = LOCATE(">", str, start);
           IF (!end) THEN SET end = start; END IF;
           SET str = TRIM(INSERT(str, start, end - start + 1, ""));
           END LOOP;
           END');
Lev Buchel
  • 532
  • 5
  • 14
Pankaj Garg
  • 1,272
  • 15
  • 21
1

EDIT:

This is multiple statement. The first part is DROP FUNCTION MyFunc IF EXISTS and the second is CREATE FUNCTION.

This was my previous answer, which was correct, but OP changed the code:

You don't say what $object->db is:

You call connectPDO("user","pass"); and then use $object->db->exec($sqlPreliminary);. $object should be global in function body. At the moment it is not, so it is a local variable, which is lost after end of the function. This $object in rest of the code is something different.

Voitcus
  • 4,463
  • 4
  • 24
  • 40
0

I have faced the same problem. When I removed " DELIMITER // " (also // DELIMITER ; at the end), it worked. I think DELIMITER is no longer required with PHP PDO.

$sqlPreliminary = "
    DROP FUNCTION IF EXISTS myFunc;
   
    CREATE FUNCTION myFunc (id INT)
        RETURNS DECIMAL

        BEGIN
        RETURN id + 1;
        END 

";

I tested this and worked.

Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
Joshy Francis
  • 340
  • 7
  • 13