SQL数据库存储/检索图片失败,代码触发mysql函数参数异常警告
Why You're Seeing Those Warnings
The two warnings stem from mixing deprecated mysql_* functions with modern mysqli_* functions—they’re not compatible! Here’s the breakdown:
mysql_select_db()expects its first parameter to be a database name (string), but you passed amysqliconnection object (frommysqli_connect()).mysql_query()expects an SQL string as its first parameter, but you likely passed the connection object instead (again, mixing function families).
On top of that, mysql_* functions are no longer supported in PHP 7+, so we’ll switch entirely to mysqli_* for a secure, working solution.
Step-by-Step Fixes
1. Correct Database Connection
Replace your existing connection code with this consistent mysqli version (it eliminates the need for a separate select_db call):
// Database credentials $host = "localhost"; $username = "your_db_user"; $password = "your_db_pass"; $dbname = "your_db_name"; // Create connection and select database in one step $conn = mysqli_connect($host, $username, $password, $dbname); // Check for connection errors if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
2. Fix Image Upload Logic
First, ensure your database table has a column of type BLOB (or MEDIUMBLOB for larger images) to store the image data. Then update your form and processing code:
<!DOCTYPE html> <html> <body background="bla.jpg"> <h3>Register with us</h3> <form enctype="multipart/form-data" method="POST"> <input type="file" name="image" /> <input type="submit" name="submit" value="Upload Image" /> </form> </body> </html> <?php if (isset($_POST['submit']) && isset($_FILES['image'])) { // Check if upload was successful if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) { die("Upload failed with error code: " . $_FILES['image']['error']); } // Sanitize image data to prevent SQL injection $imageName = mysqli_real_escape_string($conn, $_FILES['image']['name']); $imageData = mysqli_real_escape_string($conn, file_get_contents($_FILES['image']['tmp_name'])); // Insert image into database $sql = "INSERT INTO your_table_name (image_name, image_data) VALUES ('$imageName', '$imageData')"; if (mysqli_query($conn, $sql)) { echo "Image uploaded successfully!"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } } // Close connection when done mysqli_close($conn); ?>
3. Key Improvements & Checks
- Replace
your_db_user,your_db_pass,your_db_name, andyour_table_namewith your actual database details. - Add file type validation to block malicious uploads (optional but recommended):
$allowedTypes = ['image/jpeg', 'image/png']; if (!in_array($_FILES['image']['type'], $allowedTypes)) { die("Only JPG and PNG files are allowed."); } - Use
MEDIUMBLOBinstead ofBLOBif you’re uploading images larger than 64KB.
Retrieving Stored Images
To display an image later, create a separate file (e.g., get_image.php) to fetch and output the data:
<?php $conn = mysqli_connect("localhost", "your_db_user", "your_db_pass", "your_db_name"); $imageId = $_GET['id']; // Use your table's unique ID column $sql = "SELECT image_data, image_name FROM your_table_name WHERE id = $imageId"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_assoc($result); // Set correct MIME type based on file extension $extension = pathinfo($row['image_name'], PATHINFO_EXTENSION); $mime = ($extension === 'png') ? 'image/png' : 'image/jpeg'; header("Content-Type: $mime"); echo $row['image_data']; mysqli_close($conn); ?>
Display it in your HTML like this: <img src="get_image.php?id=1" alt="Uploaded Image">
内容的提问来源于stack exchange,提问作者happyswine44

