Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In fact, <strong>neither</strong> of these syntaxes is best, when it comes to writing a SQL query.</p> <p>If you're writing a SQL query, you are <em>far</em> better off writing it using parameterised queries, like so:</p> <pre><code>$query = 'SELECT * FROM contacts WHERE contact_id = ?'; $prep = $mysqli-&gt;prepare($query); //prepares the query for action $prep-&gt;bind_param("i", $id); //inserts the $id variable into the '?' as an integer value. </code></pre> <p>...or similar methods using the PDO library.</p> <p>Doing queries this way will make your queries more secure.</p> <p>Please note that if you're using the old style <code>mysql_xx()</code> functions (which do not support this style of code), these are considered obsolete and insecure, and will be removed from a future version of PHP. You should stop using them as soon as possible. (see the PHP manual for more info on this)</p> <p>It wasn't clear from the question whether you were asking about string syntax or query writing style. The above helps with query writing, and also avoid the issue with string syntax, but in case you still want to know about the string syntax issues, I will continue with some thoughts on that topic too....</p> <p>Both syntaxes are perfectly valid. The short answer is that it's fine either way, so do it whichever way works best for you in any given bit of code.</p> <p>You mentioned that your first syntax "often does not work". It would be helpful if you could elaborate on that, because it is perfectly valid PHP syntax.</p> <p>Reasons it may fail are if you have other words joining onto variable names so PHP, or if you are trying to use an array element as the variable. In this case, you should wrap your variable names in braces like so:</p> <pre><code>$string = "this is a string with a {$variable} in it"; </code></pre> <p>In fact, this works in all cases (and also helps make it clearer when you're using a variable in a string), so is best to do it all the time.</p> <p>Some people will say that using single quotes is better for performance. It is.... but the difference is very marginal, and in fact, when you're concatenating a lot of variables it becomes even less. To be honest, if they're that worried about performance that this kind of thing is an issue for them then they shouldn't be using an interpreted language like PHP.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload