php-mysqlHow to store a BLOB in a MySQL database using PHP?
Storing a BLOB (Binary Large Object) in a MySQL database using PHP can be done by using the mysqli_query()
function. The following example code will store a BLOB in the files
table of the mydb
database:
$conn = mysqli_connect("localhost", "my_user", "my_password", "mydb");
$sql = "INSERT INTO files (name, type, size, content ) ".
"VALUES ('$fileName', '$fileType', '$fileSize', '$content')";
$result = mysqli_query($conn, $sql);
The output of the above code will be a TRUE
or FALSE
value, depending on whether the query was successful or not.
Code explanation
mysqli_connect()
: Establishes a connection to the MySQL database.$sql
: The SQL query used to insert the BLOB into the database.$result
: Stores the result of the query.mysqli_query()
: Executes the SQL query.
Helpful links
More of Php Mysql
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to join tables with PHP and MySQL?
- How to get the version of MySQL using PHP?
- How to write an update query in MySQL using PHP?
- How to count the number of resulting rows in a MySQL database using PHP?
- How to use a MySQL union in PHP?
- How to set a timeout for MySQL query in PHP?
- How to create an SSL connection to MySQL using PHP?
- How to get the last insert ID in PHP MySQL?
- How to fetch data from MySQL in PHP?
See more codes...