Jump to content

How to insert string with dollar sign ($) into mysql table?


colap

Recommended Posts

$_SERVER['argv'][1'] = "$GGGabcd";
$query = "insert into table1 (str) VALUES(' " . $_SERVER['argv'][1] . " ')";

 

It's a string that starts with a dollar sign. So how can i insert this string into mysql table with php?

How can i escape or something to add that string with $ (dollar) ?

mySql does not treat the $-sign specially. However, if you happen to have a PHP variable named $GGGabcd (you should change the name to something meaningful), PHP will interpret the variable when you include it in a double-quoted string. Use single quotes:

 

$_SERVER['argv']['1'] = '$GGGabcd';

 

…if you happen to have a PHP variable named $GGGabcd

 

Variable interpolation will still occur regardless of whether the variable exists or not.

 


php-coder, as DavidAM showed you should be using single-quotes around that string (or backslash-escaping the dollar).

@salathe: I did not realize that. I almost never use double-quoted strings. But I see you are correct, this code produces an "Undefined variable" notice. Yet another reason to avoid double-quoted strings. And to make sure error reporting is on during development.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.