Jump to content

Ignore ' in Query


Mko

Recommended Posts

I have the following Query:

$query = "INSERT INTO characters SET psalt = '" .$salt. "'"; 

 

However, when the salt is generated, it may contain a ', thus stopping the Query.

I need to know how I could make it so the Query would be unaffected by the ' in the salt value.

 

Thanks!

Link to comment
Share on other sites

I might be wrong....but i am not sure if you escape a single quote, the slash will get stored in database.

It just tells MySQL to not consider the single quote as a syntax

but again i might be wrong..

 

mysql> INSERT INTO `temp` (last_name) values ('sam\'shel');

Query OK, 1 row affected (0.00 sec)

 

mysql> SELECT * FROM temp\G

*************************** 1. row ***************************

        last_name: sam'shel

Link to comment
Share on other sites

Use prepared statements.

 

Why exactly do you need quotes in your salt?

Well, the salts are randomly generated, and on occasion, may contain a '.

Then, the salts are combined with the hash that's generated elsewhere to produce the password. This password is referenced with the user input to determine if it's a match.

Link to comment
Share on other sites

I might be wrong....but i am not sure if you escape a single quote, the slash will get stored in database.

It just tells MySQL to not consider the single quote as a syntax

but again i might be wrong..

 

mysql> INSERT INTO `temp` (last_name) values ('sam\'shel');

Query OK, 1 row affected (0.00 sec)

 

mysql> SELECT * FROM temp\G

*************************** 1. row ***************************

        last_name: sam'shel

 

Hmm, perhaps it does not then, I thought it did.

 

I've used only prepared statements for a long time. *shrug*

Link to comment
Share on other sites

I might be wrong....but i am not sure if you escape a single quote, the slash will get stored in database.

It just tells MySQL to not consider the single quote as a syntax

but again i might be wrong..

 

mysql> INSERT INTO `temp` (last_name) values ('sam\'shel');

Query OK, 1 row affected (0.00 sec)

 

mysql> SELECT * FROM temp\G

*************************** 1. row ***************************

        last_name: sam'shel

 

Wohoo, it works!

Thanks a lot everyone, you've all been a tremendous help!

Link to comment
Share on other sites

The same as you would for any data that may contain content that could foul the query: mysql_real_escape_string()

As a result, would the \ be inserted into the salt value in the database as well?

 

Yes.

 

Fail :P

 

That's the whole point of mysql_real_escape_string() - it escapes characters so they will be interpreted as literal characters in the query and not control characters (e.g. quotes around a value)

 

Example:

$value = "foo'bar";
$sql_value = mysql_real_escape_string($value);

$query = "INSERT INTO `table` (`field`) VALUES ('$sql_value')";
echo $query; //Output: INSERT INTO `table` (`field`) VALUES ('foo\'bar')

If that query is run the actual value inserted into the database will be foo'bar, without the backslash. It's not really any different than escaping quotemarks that are included in a quoted string

echo "<input type=\"text\" name=\"email\" />";

 

Link to comment
Share on other sites

The same as you would for any data that may contain content that could foul the query: mysql_real_escape_string()

As a result, would the \ be inserted into the salt value in the database as well?

 

Yes.

 

Fail :P

 

Meh. :P

 

Like I said, it's been a long time since I've worked that way. I remember having to stripslashes data from a database at one point... must have been for some other reason.

Link to comment
Share on other sites

Like I said, it's been a long time since I've worked that way. I remember having to stripslashes data from a database at one point... must have been for some other reason.

 

Probably using mysql_real_escape_string() on inserted values that were first run through magic quotes.

Link to comment
Share on other sites

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.