inner join wont work with mysqli prepared statement in php

五迷三道 提交于 2019-12-25 04:14:18

问题


I can't seem to get this statement or statements alike to work with prepared queries, the code works just fine below:

$DBH = getDBH();
    $stmt = $DBH->prepare("SELECT a.id, a.title, a.photo FROM tag t INNER JOIN tag_reference atx ON t.tag_id = atx.tag_id
        INNER JOIN articles a
            ON atx.article_id = a.id
    WHERE t.tag_name = 'example'");
    $stmt->execute();
    $stmt->bind_result($id,$title,$photo);
    $stmt->fetch();

but when I change t.tag_name = '?' it gives me an error that the amount of parameters do not match. This is the statement that does not work.

$DBH = getDBH();
    $stmt = $DBH->prepare("SELECT a.id, a.title, a.photo FROM tag t INNER JOIN tag_reference atx ON t.tag_id = atx.tag_id
        INNER JOIN articles a
            ON atx.article_id = a.id
    WHERE t.tag_name = '?'");
        $stmt->bind_param('s',$example);
    $stmt->execute();
    $stmt->bind_result($id,$title,$photo);
    $stmt->fetch();

Can anyone please help?


回答1:


The placeholder ? does not work if enclosed in single quotes. In this case the SQL tokenizer will catch it as literal string.

Change it to:

     WHERE t.tag_name = ? ");



回答2:


When using placeholders, do you need to use quotes? Most placeholder languages I've used don't.

WHERE t.tag_name = ?"


来源:https://stackoverflow.com/questions/5399623/inner-join-wont-work-with-mysqli-prepared-statement-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!