When to use single quotes, double quotes, and backticks in MySQL

后端 未结 13 1037
一个人的身影
一个人的身影 2021-01-26 10:55

I am trying to learn the best way to write queries. I also understand the importance of being consistent. Until now, I have randomly used single quotes, double quotes, and backt

13条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-26 11:25

    In combination of PHP and MySQL, double quotes and single quotes make your query-writing time so much easier.

    $query = "INSERT INTO `table` (`id`, `col1`, `col2`) VALUES (NULL, '$val1', '$val2')";
    

    Now, suppose you are using a direct post variable into the MySQL query then, use it this way:

    $query = "INSERT INTO `table` (`id`, `name`, `email`) VALUES (' ".$_POST['id']." ', ' ".$_POST['name']." ', ' ".$_POST['email']." ')";
    

    This is the best practice for using PHP variables into MySQL.

提交回复
热议问题