I want to save a json string in MySQL table. The json string comes from a canvas (using fabricjs).
var jsonCanvas = JSON.stringify(canvas.toDatalessObject());
My first question is, what is the best datatype for saving the json string in MySQL? The Canvas will contain images as well as text and other objects.
Do you think MEDIUMTEXT is enough?
The other question.
Is it possible to save the json string with this prepared statement. I’m doubtful to use the keyword “s” for string.
try {
$sql = "insert into layout (lt_author, lt_date, lt_category, lt_json_string)
values (?,?,?,?)";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("ssss",$lt_author, $lt_date, $lt_category, $lt_json_string);
$stmt->execute();
$stmt->close();
Thank you for helping 🙂
Greetings Max
I’m assuming that your SQL is correct.
I always chose
TEXTto store my json string representation of my canvas.But there is somethings to consider before you chose how to save your JSON representation.
See this discussion here: Loading JSON in canvas with fabric.js
It’s explained the main points that you need observe.
.toDatalessObject()instead.toJson()method:– Pros: As kangax explained in the discussion, toDatalessObject will save just the url source of your svg, so, less data to be saved in your database. By this way, you can maybe use other type to store your json canvas representation (maybe you can use a
TINYTEXTtype)– Cons: You need have the files in your server, because you will reference it when load the canvas again.
You need know because, if you use
.toDatalessJson()method and you will have more svg loaded in your canvas, this svg will be converted inPathandPathGroupobject, and will be save in your database.So, if you have many, many svg in your canvas, maybe the final json string can overflow the size allowed for types
TEXTin database (if this happen, there some solutions, like split your json string representation and save in two or more entries in your database).But, if this happen frequently, maybe will better chose other type, like
MEDIUMTEXT, as you suggest.