suneet
suneet

Reputation: 400

Insert huge amount of data in mysql

So I am trying to store user likes from facebook in database

$likes_json=json_encode($user->likes->data);
echo($likes_json);
$query = "INSERT INTO users VALUES (' ','".$likes_json."', NOW())";
$mysqli->query($query);

"echo" prints out the expected string (of length 56000 characters)

[{"name":"The Next Web","category":"Website","id":"106976563522"....

But nothing is being stored in Database. When I change $likes_json='some other string'; it works fines(it gets inserted in the database)

Coloumn for likes is of type "mediumtext"

Upvotes: 1

Views: 227

Answers (2)

NoPyGod
NoPyGod

Reputation: 5067

You need to escape $likes_json using mysql_real_escape_string

Your query ends up looking like this

INSERT INTO users VALUES (' ','[{"name":"The Next Web","category":"Website","id":"106976563522"}]', NOW())

See why that wouldn't work? What if the data contains a ' ?

Upvotes: 1

Ibu
Ibu

Reputation: 43850

The error can be escaping, but the only way to know for sure is to print the error:

$mysqli->query($query) or die("Error: ":$mysqli->error);

Escaping:

$likes_json=json_encode($user->likes->data);
$query = "INSERT INTO users VALUES (' ','%s', NOW())";
$query = sprintf($query,$mysqli->real_escape_string($likes_json);

Upvotes: 1

Related Questions