MySQLi 准备好的语句不采用调用过程的有效查询

MySQLi prepared statement not taking valid query that calls a procedure

$prepared = $db->prepare("
        SET @content = ?;

        CALL add_interest_if_not_exists( @content );

        SET @iid = (SELECT interests_id
                    FROM interests
                    WHERE content = @content);

        REPLACE INTO profile_interests (user_id, interests_id, likes)
        VALUES (
            ?,
            @iid,
            ?
        )
    ");
echo $db->error;

错误是:

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 'CALL add_interest_if_not_exists( @content ); SET @iid = (SELECT i' at line 3

查询使用 MySQL workbench(硬编码值)。为什么它不会出现在我准备好的声明中...

这不是单个查询,而是一组查询。

因此你必须 运行 他们一个接一个,每个都有单独的 query()prepare()/execute() 调用。

为什么不使用存储过程?

CREATE PROCEDURE add_user_interest(p_content varchar(200), p_user_id int,p_likes int)
BEGIN 

    CALL add_interest_if_not_exists( p_content );

    SET @iid = (SELECT interests_id
                FROM interests
                WHERE content = p_content);

    REPLACE INTO profile_interests (user_id, interests_id, likes)
    VALUES (
        p_user_id,
        @iid,
        p_likes
    );

END

结束你将作为单个准备好的语句调用

$prepared = $db->prepare("CALL add_user_interest( ?, ? , ? )");