开发者

PHP Mysql select from variable problem [duplicate]

开发者 https://www.devze.com 2023-02-25 08:15 出处:网络
This question already has an answer here: 开发者_C百科 Syntax error due to using a reserved word as a table or column name in MySQL
This question already has an answer here: 开发者_C百科 Syntax error due to using a reserved word as a table or column name in MySQL (1 answer) Closed 8 years ago.

I have tried so many different soloutions but cannot get this to work Here is my code:

$to = $_POST['to'];
$query = "SELECT to FROM to WHERE to='$to' " 
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)){

I get a whole load of different errors every time i modify it. At the moment I'm getting

You have an error in your SQL syntax near to='Name'

When I modify it to fix this I get

mysql_fetch_array() not valid

It seems when using variables it messes up

can anyone help?

Thanks!


to is a reserved word in mySQL.

You would have to wrap each mention of the table or column name into backticks

SELECT `to` from `to`

but it would be vastly better to use a different name.


To is a Reserved keyword try escaping it by using "``" symbol

Check this Link

Reserved Keywords MYSQL


Consider changing the names of your field and table (Edit: Definitely change the names or at least escape them.) Also, all you are doing is selecting the variable you already have.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号