Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert special characters into a database?

Can anyone tell me how to insert special characters into a MySQL database? I've made a PHP script which is meant to insert some words into a database, although if the word contains a ' then it wont be inserted.

I can insert the special characters fine when using PHPmyAdmin, but it just doesn't work when inserting them via PHP. Could it be that PHP is changing the special characters into something else? If so, is there a way to make them insert properly?

like image 320
Joey Morani Avatar asked Apr 06 '10 10:04

Joey Morani


People also ask

How do I use special characters in SQL?

Use braces to escape a string of characters or symbols. Everything within a set of braces in considered part of the escape sequence. When you use braces to escape a single character, the escaped character becomes a separate token in the query. Use the backslash character to escape a single character or symbol.

Does VARCHAR allow special characters MySQL?

So what is varchar in SQL? As the name suggests, varchar means character data that is varying. Also known as Variable Character, it is an indeterminate length string data type. It can hold numbers, letters and special characters.

How do I save a special character in SQL?

Use NVARCHAR instead of VARCHAR. SQL Server provides both datatypes to store character information. For the most part the two datatypes are identical in how you would work with them within SQL Server or from an application.

What are symbols and special characters?

A special character is one that is not considered a number or letter. Symbols, accent marks, and punctuation marks are considered special characters. Similarly, ASCII control characters and formatting characters like paragraph marks are also special characters.


4 Answers

$insert_data = mysql_real_escape_string($input_data);

Assuming that you have the data stored as $input_data

like image 172
Anthony Avatar answered Oct 12 '22 14:10

Anthony


Are you escaping? Try the mysql_real_escape_string() function and it will handle the special characters.

like image 38
zaf Avatar answered Oct 12 '22 13:10

zaf


You are most likely escaping the SQL string, similar to:

SELECT * FROM `table` WHERE `column` = 'Here's a syntax error!'

You need to escape quotes, like follows:

SELECT * FROM `table` WHERE `column` = 'Here\'s a syntax error!'

mysql_real_escape_string() handles this for you.

like image 45
Paul Lammertsma Avatar answered Oct 12 '22 13:10

Paul Lammertsma


use mysql_real_escape_string

So what does mysql_real_escape_string do?

This PHP library function prepends backslashes to the following characters: \n, \r, \, \x00, \x1a, ‘ and “. The important part is that the single and double quotes are escaped, because these are the characters most likely to open up vulnerabilities.

Please inform yourself about sql_injection. You can use this link as a start

like image 30
Peter Parker Avatar answered Oct 12 '22 13:10

Peter Parker