Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP string concat without the "dot" operator

Tags:

php

I'm working to integrate a plug-in into a PHP web application, and one line of the code puzzles me:

$sql = "update inventory set qtyleft='$qtyleft',price='$price',sales=sales+'$sales',qtysold=qtysold+'$qtysold' where id='$id'";
mysql_query($sql);

where $qtyleft, $price, $sales, $qtysold and $id are all variables.

I'm not very familiar with PHP, but I always thought string concatenation in PHP is done by using the . operator and it seems to me that the code above is just a long string without actually putting those variables to the SQL query. Is that the case?

like image 752
Jim Avatar asked Sep 13 '26 04:09

Jim


1 Answers

In PHP, double quote (") delimited strings will evaluate variables in them.

$foo = 42;
echo "The answer for everything is $foo"; // The answer for everything is 42

This specific example is very bad because you shouldn't include variables directly in an SQL query, and shouldn't use mysql_query in new code.

See more:

  • Why shouldn't I use mysql_* functions in PHP?
  • How can I prevent SQL injection in PHP?
like image 55
Madara's Ghost Avatar answered Sep 14 '26 19:09

Madara's Ghost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!