<h2>Insert Blobs in MySql databases with php</h2>
<p>When attempting to store an image in a MySQL database, you might encounter an issue. This guide will provide solutions to successfully store your image data.</p>
<h3>Issue</h3>
<pre>
$sql = "INSERT INTO ImageStore(ImageId,Image)
VALUES('$this->image_id','file_get_contents($tmp_image)')";
</pre>
<p>This code builds a string in PHP, but the function call file_get_contents($tmp_image) is not evaluated before the string is created. Consequently, the actual binary data is not inserted.</p>
<h3>Solution 1</h3>
<p>To resolve this, you need to explicitly concatenate the result of the function call:</p>
<pre>
$sql = "INSERT INTO ImageStore(ImageId,Image)
VALUES('$this->image_id','" . file_get_contents($tmp_image) . "')";
</pre>
<h3>Solution 2</h3>
<p>Additionally, ensure that any characters in the binary data that might interfere with the query are escaped:</p>
<pre>
$sql = "INSERT INTO ImageStore(ImageId,Image)
VALUES('$this->image_id','" . mysql_escape_string(file_get_contents($tmp_image)) . "')";
</pre>
<h3>Solution 3</h3>
<p>Storing large binary data in a database is generally not recommended. It can lead to performance issues and database bloat. Consider using a separate file system to store your images instead.</p>
Clause de non-responsabilité: Toutes les ressources fournies proviennent en partie d'Internet. En cas de violation de vos droits d'auteur ou d'autres droits et intérêts, veuillez expliquer les raisons détaillées et fournir une preuve du droit d'auteur ou des droits et intérêts, puis l'envoyer à l'adresse e-mail : [email protected]. Nous nous en occuperons pour vous dans les plus brefs délais.
Copyright© 2022 湘ICP备2022001581号-3