如何将php变量插入mysql查询?

时间:2013-10-06 21:27:46

标签: php mysql sql

我有以下声明:

 SELECT bname,cnum,vnum, MATCH(vtext) AGAINST (''".$word."'') as relevance FROM kjv  WHERE MATCH(vtext) AGAINST (''".$word."'') AND bnum='".$book."' ORDER by relevance DESC, bnum, cnum, vnum LIMIT 0,1");

返回空行,但如果我用硬编码值替换变量,一切都会通过。变量不是null,我知道因为我在框中输入后将它们输出到页面。感谢

4 个答案:

答案 0 :(得分:0)

最好看到更多的周围代码,但我认为这是导致问题的双引号:

SELECT bname,cnum,vnum, MATCH(vtext) AGAINST ('".$word."') as relevance FROM kjv  WHERE MATCH(vtext) AGAINST ('".$word."') AND bnum='".$book."' ORDER by relevance DESC, bnum, cnum, vnum LIMIT 0,1");

答案 1 :(得分:0)

嗨,如果您已在变量中保存了值,请尝试此操作。

 SELECT bname,cnum,vnum, MATCH(vtext) AGAINST ('$word') as relevance FROM kjv    WHERE MATCH(vtext) AGAINST ('$word') AND bnum='".$book."' ORDER by relevance DESC, bnum, cnum, vnum LIMIT 0,1");

答案 2 :(得分:-1)

尝试

mysql_query("SELECT bname,cnum,vnum, MATCH(vtext) AGAINST($word) as relevance FROM kjv  WHERE MATCH(vtext) AGAINST ($word) AND bnum='$book' ORDER by relevance DESC, bnum, cnum, vnum LIMIT 0,1");

答案 3 :(得分:-1)

试试这样:

SELECT bname,cnum,vnum, MATCH(vtext) AGAINST ("$word") as relevance
FROM kjv  WHERE MATCH(vtext) AGAINST ("$word") AND bnum= "$book" 
ORDER by relevance DESC, bnum, cnum, vnum LIMIT 0,1");