Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does {} affect a MySQL Query in PHP? [duplicate]

Tags:

php

mysql

What is the difference between the following 2 queries?

mysql_query("UPDATE table SET name = '$name'");

mysql_query("UPDATE table SET name = '{$name}'");
like image 201
James Simpson Avatar asked Mar 20 '11 18:03

James Simpson


People also ask

Why we use curly braces in SQL?

Parentheses are used in SQL to group parameters or arguments. They are required when entering a command (i.e. they must be typed exactly as they appear). Curly braces indicate groupings of identifiers, parameters, or arguments.

How can we avoid inserting duplicate records in MySQL using PHP?

Note − Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.

How do I exclude duplicate values in MySQL?

mysql> SELECT DISTINCT last_name, first_name -> FROM person_tbl -> ORDER BY last_name; An alternative to the DISTINCT command is to add a GROUP BY clause that names the columns you are selecting. This has the effect of removing duplicates and selecting only the unique combinations of values in the specified columns.

How can we prevent insertion of duplicate data?

Instead of INSERT you should try using MERGE statement instead. It already takes care of identifying duplicates and provides separate execution paths for the two cases (duplicate and non-duplicate).


3 Answers

ON the SQL side, there is absolutely no difference : the two queries are exactly the same.
(you can check that by echo-ing them)

{$variable} is a more complete syntax of $variable, that allows one to use :

  • "this is some {$variable}s"
  • "{$object->data}"
  • "{$array['data']}"
  • "{$array['data']->obj->plop['test']}"


For more informations, you should read the Variable parsing / Complex (curly) syntax section of the manual (quoting a few bits) :

This isn't called complex because the syntax is complex, but because it allows for the use of complex expressions.

Any scalar variable, array element or object property with a string representation can be included via this syntax.
Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }.

like image 59
Pascal MARTIN Avatar answered Nov 02 '22 13:11

Pascal MARTIN


The curly braces "escape" the PHP variable and are not passed to MySQL. With a simple variable like $name it doesn't make a difference but with something like $user['name'] it does. So there is nothing different between the two queries you have posted in your question.

like image 42
Treffynnon Avatar answered Nov 02 '22 13:11

Treffynnon


This query can be used if you want to pass a single variable:

mysql_query("UPDATE table SET name = '$name'");

This can be used if you are passing a value from an array's particular index.

mysql_query("UPDATE table SET name = '{$1}'",$name);

By the way your both queries were also correct in their means.

like image 26
Sujit Agarwal Avatar answered Nov 02 '22 12:11

Sujit Agarwal