I have a table called comments in MySQL -
+-----------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | | 0 | |
| data | varchar(255) | YES | | NULL | |
| createdOn | timestamp | NO | | CURRENT_TIMESTAMP | |
+-----------+--------------+------+-----+-------------------+----------------+
I use CodeIgniter PHP framework. My PHP code that inserts a row in above table -
$comment = null;
if (isset($_POST['comment']) {
$comment = $_POST['comment'];
}
$comment = mysql_real_escape_string($comment);
$input = array(
'comment' => $comment,
);
$data = json_encode($input);
$sql = "insert into comments(data, user_id) values ('$data', $user_id)";
log_message("info", "add sql :: $sql");
$this->db->query($sql);
Now the same code is deployed on two server.
- server 1
PHP 5.3
MySQL 5.5 - server 2
PHP 5.4
MySQL 5.5
When I give below input for a comment, it causes an SQL error -
hello'
Error -
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '321)' at line 1
insert into comments(data, user_id) values ('{"comment":"hello\\'"', 321)
Filename: /home/hussain/workspace/app/CodeIgniter_2.1.0/models/test_model.php
Line Number: 32
I get above error on server1 but not on server2. Why?