dongqieli4164 2016-03-04 02:38
浏览 28

奇怪的mysqli未知的错误? [重复]

This question already has an answer here:

can someone see the differences between this

$query = "INSERT INTO users ('username', 'password', 'email', 'firstname', 'lastname', 'user_level') VALUES ('$username', '$password', '$email', '$firstname', '$lastname', '1')";

and this

$query_2 = "INSERT INTO users (`username`, `password`, `email`, `firstname`, `lastname`, `user_level`) VALUES ('$username', '$password', '$email', '$firstname', '$lastname', '1')";

only the second one works but the first one doesn't but it look exactly the same to me.

</div>
  • 写回答

2条回答 默认 最新

  • douxian6086 2016-03-04 02:47
    关注

    The difference between the two are simple.

    One is using regular quotes ' around the column names that are mostly used to identify string literal values, as opposed to backticks which are used for escaping table/column names.

    Read up on string literals and identifier qualifiers:

    Those are things you need to know the difference for, when dealing with databases.

    Backticks are mostly used whenever a table/column name contains a space, a hyphen, or is a MySQL reserved word.

    Sidenote: If none of your column names contains spaces, hyphens or isn't a reserved word, you can just omit the ticks altogether.

    Reference:

    Using this example would fail.

    SELECT column_name FROM my-table
    

    would produce the following error:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-

    What is happening here is that, my-table would be interpreted as my MINUS table and MySQL thinks you want to do a mathematical problem.

    This would be the same scenario for your INSERT query.

    • The INSERT would produce the following error:

    Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''username'

    Whereas:

    SELECT `column 1` FROM `my-table`
    

    with backticks would be valid and won't produce syntax errors.

    The first line of code would have produced syntax errors.

    "weird mysqli unknown mistake?"

    • It's only "weird" when things are unclear/unknown.

    Important note about using quotes or a space in SELECT:

    SELECT user name FROM table
    

    would produce the following:

    Unknown column 'user' in 'field list'

    while SELECT 'username' FROM table WHERE username='".$username."'

    would not produce an error, but strangely enough give you an entirely different result than expected.

    In regards to the values you are presently using, may be leaving you open to an SQL injection.

    If you haven't properly escaped those values, use a prepared statement.

    References:

    Error checking.

    Depending on the MySQL API used to connect with which is unknown, checking for errors would have thrown you something about it.

    Use the respective method:

    An mysqli_ example:

    mysqli_query(...) or die(mysqli_error($connection));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击