Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>$image_data</code> is a binary string. <code>-&gt;escape</code> may not work on it, since it may escape random bytes in it, thus leaving you with a corrupted image. Also the binary string may contain quote characters (or other characters) that is making your query invalid.</p> <p>Try to encode the binary string as hex before inserting into MySQL. You can use PHP's <a href="http://php.net/bin2hex" rel="nofollow"><code>bin2hex</code></a> for this.</p> <pre><code>$escaped_user_id = $my_db-&gt;escape($user_id); $hex_image = bin2hex($image_data); $stmt = "INSERT INTO my_table (UserID, ImageData) VALUES ({$escaped_user_id}, X'{$hex_image}')"; </code></pre> <p>The <code>X</code> in <code>X{$hex_image}</code> is how MySQL handles literal hex strings: <a href="http://dev.mysql.com/doc/refman/5.1/en/hexadecimal-literals.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.1/en/hexadecimal-literals.html</a></p> <p>If that doesn't work, you can also try <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_unhex" rel="nofollow"><code>UNHEX()</code></a>.</p> <pre><code>$escaped_user_id = $my_db-&gt;escape($user_id); $hex_image = bin2hex($image_data); $stmt = "INSERT INTO my_table (UserID, ImageData) VALUES ({$escaped_user_id}, UNHEX('{$hex_image}'))"; </code></pre> <p><strong>EDIT</strong>: I didn't notice you were using MSSQL and not MySQL. My bad. In MSSQL, you can insert literal hex strings with <code>0x</code>.</p> <pre><code>$escaped_user_id = $my_db-&gt;escape($user_id); $hex_image = bin2hex($image_data); $stmt = "INSERT INTO my_table (UserID, ImageData) VALUES ({$escaped_user_id}, 0x{$hex_image})"; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload