Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.