$reponse = $bdd->prepare('select * from student where UserName = ? and Password = ? ');
$reponse->execute(array($name,$pass));
这是工作,但是当我想用php变量替换'student'时,它不起作用:/ 看:
$table="stackoverflow"
$reponse = $bdd->prepare('select * from '$table' where UserName = ? and Password = ? ');
$reponse->execute(array($name,$pass));
你有个主意吗?如果我可以用变量替换表名,那将更简单。 对不起,我的英语不好。 谢谢你花在我身上的时间。
答案 0 :(得分:5)
改变这个:
$reponse = $bdd->prepare('select * from '$table' where UserName = ? and Password = ? ');
要:
$reponse = $bdd->prepare("select * from `$table` where UserName = ? and Password = ? ");
答案 1 :(得分:3)
在PHP中,您可以用句点字符(。)连接两个字符串或变量。
因此,要在表名之前和之后连接表名和语句,您应该更改此行
$reponse = $bdd->prepare('select * from '$table' where UserName = ? and Password = ? ');
到
$reponse = $bdd->prepare('select * from '.$table.' where UserName = ? and Password = ? ');
答案 2 :(得分:0)
由于表名是字符串,请尝试 -
"SELECT * FROM from \"".$table."\" WHERE ....... ";