我的查询有点像这样:
CREATE FUNCTION func ... ;
SELECT * FROM ....;
我正在使用 php PDO
,它不允许 a 中的语句超过 1 个$pdo->query()
,所以我决定将查询分成两部分,所以我CREATE FUNCTION
使用执行查询$pdo->exec()
,然后$pdo->query()
在 select 语句上使用。
但是,我在执行 select 语句时收到错误FUNCTION database.func does not exist
。我该如何解决这个问题?当我在 phpmyadmin 中将它作为单个查询运行时,它工作正常
编辑:PHP代码:
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));