PHP上传图片后显示错误 "mysql extension is deprecated"

标签 php mysql mysqli

我已成功在 MYSQL 中上传图片,但是图片上传后出现以下错误。

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";
    }
}
}

最佳答案

这是因为您使用的 PHP 版本不支持 mysql_* 函数,而需要使用 mysqli_* 函数。 (http://php.net/manual/en/mysqli.summary.php)

您的代码将如下所示:

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";
    }
}
}

我个人的建议是使用 PDO ( http://php.net/manual/en/book.pdo.php )

关于PHP上传图片后显示错误 "mysql extension is deprecated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31542191/

相关文章:

MySQL 连续查找上一集?

javascript - 检测表单何时来自灯箱或直接来自页面

php - Zend Form 复选框已选中

mysql - 附加外键后无法删除行

mysql - 在 mysql select with as 中做数学运算

php - MariaDB 和 PHP - 查询无法正常工作

php - 警告 : max() [function. 最大值] : Array must contain at least one element - whats is this?

php - 第 1 部分 : jQuery -> MySQL -> jQuery -> HTML

php - 从一个 php 文件生成时的自定义 URL

php - 当对两列使用 mysqli_multi_query 时,排序无法正常工作