Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a name with ' in a MySQL table

Tags:

php

mysql

insert

First time with this trouble when dealing with a MySQL table.

I'm inserting bar names in a table. If the bar is called "Tim's Bar" and I insert it straight away I get an error and the data is not inserted.

How do you instert properly the ' in the table?

like image 373
user712027 Avatar asked Dec 08 '25 10:12

user712027


2 Answers

Use mysql_real_escape_string():

http://php.net/manual/en/function.mysql-real-escape-string.php

like image 138
magma Avatar answered Dec 10 '25 01:12

magma


Use PDO with prepared statements.

$query = $pdo->prepare('INSERT INTO bars (name) VALUES (?)');
$query->execute("Tim's Bar");

It's superior (and safer) than using the mysql(i)_* family of functions directly.

like image 28
Sander Marechal Avatar answered Dec 10 '25 01:12

Sander Marechal