dongzi1209 2015-07-21 14:39
浏览 40
已采纳

PHP上传图片后显示错误“mysql extension is deprecated”[复制]

This question already has an answer here:

I have successfully uploaded a image in MYSQL, however after the image is uploaded i get the below error.

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in x:\xxx\xxxx\xxxx\upload.php on line 6 done

if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
    mysql_connect("localhost", "root", "root");
    mysql_select_db ("test");
    $imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
    $imageProperties = getimageSize($_FILES['userImage']['tmp_name']);

    $sql = "INSERT INTO output_images(imageType ,imageData)
    VALUES('{$imageProperties['mime']}', '{$imgData}')";
    $current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
    if(isset($current_id)) {
        echo "done";
    }
}
}
</div>
  • 写回答

1条回答 默认 最新

  • douyou8266 2015-07-21 14:42
    关注

    It's because you are using a version of PHP that does not support mysql_* functions, instead you need use the mysqli_* functions. (http://php.net/manual/en/mysqli.summary.php)

    Your code will looks like this:

    if(count($_FILES) > 0) {
    if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
        mysqli_connect("localhost", "root", "root");
        mysqli_select_db ("test");
        $imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
        $imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
    
        $sql = "INSERT INTO output_images(imageType ,imageData)
        VALUES('{$imageProperties['mime']}', '{$imgData}')";
        $current_id = mysqli_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error());
        if(isset($current_id)) {
            echo "done";
        }
    }
    }
    

    My personal recommendation is use PDO (http://php.net/manual/en/book.pdo.php)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?