简体   繁体   中英

Can't replace table name with variable in SQL query

i've a little problem. Here my request ( it work ):

 $reponse = $bdd->prepare('select * from student where UserName = ? and Password = ? ');
 $reponse->execute(array($name,$pass));

It's work but when i want to replace 'student' by a php variable it's doesn t works :/ Look :

 $table="stackoverflow"
 $reponse = $bdd->prepare('select * from '$table' where UserName = ? and Password = ? ');
 $reponse->execute(array($name,$pass));

Have you an idea ? It will be more simple if i can replace the table name by a variable. sorry for my bad english. Thanks you for the time you spend to me.

Change this:

 $reponse = $bdd->prepare('select * from '$table' where UserName = ? and Password = ? ');

To:

 $reponse = $bdd->prepare("select * from `$table` where UserName = ? and Password = ? ");

In PHP, you join two strings or variables by a period character (.).

Therefore, to concatenate the table name and your statements before and after the table name, you should be changing this line

$reponse = $bdd->prepare('select * from '$table' where UserName = ? and Password = ? ');

To

$reponse = $bdd->prepare('select * from '.$table.' where UserName = ? and Password = ? ');

由于表名是字符串,请尝试-

"SELECT * FROM from \"".$table."\" WHERE ....... ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM