First post, so if this is wrong pls forgive: this has had me baffled for a few days now. I’ve declared a mysql (mariadb actually) database function as follows:
CREATE FUNCTION f_is_collection
(
trid INT
)
RETURNS INT
BEGIN
RETURN (SELECT COUNT(*) FROM assoc_title_title WHERE collection_rid = trid);
END;
It’s meant to query an associative table that contains columns ‘collection_rid’ and ‘content-rid’, where a collection record id (say - a book) maps to its content record ids (say - short stories). It works as expected directly from the database: SELECT f_is_collection(1) returns 0, since record id 1 is a short story, while SELECT f_is_collection(100) returns 8, specifying that record 100 contains 8 short story titles. Well & good.
When I call this function from php as below:
function fn_is_collection($trid) {
global $dbh;
try {
$qry = "SELECT f_is_collection(?)";
$stmt = $dbh->prepare($qry);
$stmt->bindParam(1, $trid);
$stmt->execute();
var_dump($stmt->fetch(PDO::FETCH_BOTH));
var_dump($dbh->errorInfo());
}
catch ... etc
I get this:
bool(false) array(3) { [0]=> string(5) “00000” [1]=> NULL [2]=> NULL } NULL
I’ve tried fetch(), fetchAll(), fetch(PDO::FETCH_ATTR), fetch(PDO::FETCH-BOTH) - all to no avail. In desperation, I even converted the db function to a stored procedure: same result.
Any kind help is greatly appreciated.
…Apologies: php 7.4.3, mariadb 10.3.34, xubuntu 20.04.1