我的MYSQL查询中需要在哪里使用反引号或引号? [英] Where do I need to use backticks or quotes in my MYSQL query?
问题描述
我正在尝试运行以下查询,但是我不确定我的值是否应该为$ s,$ form_id =记录的列,$ user_id是名为cf_id的记录的主键.
I am trying to run the following query, but I am not sure if my 's should be `s or not, $form_id = the record's column , $user_id is the primary key of the record called cf_id .
$ querydate稍后将在脚本中回显,因为它从记录中拉出等于$ form_id和$ user_id的日期.
$querydate is going to be echo'd later on in the script, as it pulls the date from the record that equals to $form_id and $user_id .
$querydate = mysql_query("SELECT '$form_id' FROM email_history WHERE cf_id = '$user_id'") or die(mysql_error());
编辑>>>>>>
尝试了以下一些解决方案后,它似乎可以正常工作,但是与其获取日期的格式名称,不如存储日期,而是获取了此回显信息,所以我不确定现在发生了什么: :资源ID#120 :资源ID#121 :资源ID#122 :资源ID#123
After trying some of the solutions below, it seems to work ok, but instead of getting the date stored under the form name, I am getting this echo'd instead, so im not sure whats happening now : :Resource id #120 :Resource id #121 :Resource id #122 :Resource id #123
该表的设置如下:
[USER_ID] [FORM_ID1212212] [FORM_ID1212112]
[1] [2-1-2012] [2-1-2012]
[2] [1-1-2012] [1-1-2012]
推荐答案
对于表和列名称,请使用反引号(`),对于字符串,请使用单引号(').
You use backticks (`) for table and column names, single quotes (') for strings.
$querydate = mysql_query("SELECT `$form_id` FROM email_history WHERE cf_id = '$user_id'");
仅当您的表名或列名是MySQL保留字时才需要反引号.最佳实践是避免保留字
Backticks are only needed when your table name or column name is a MySQL reserved word... best practise is to avoid reserved words
但是也可以考虑切换到PDO并使用准备好的语句,或者至少切换到mysqli而不是mysql
But also consider switching to PDO and using prepared statements, or at least to mysqli rather than mysql
这篇关于我的MYSQL查询中需要在哪里使用反引号或引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!