php mysql_connect():mysql 扩展已弃用,将来会被移除:改用 mysqli 或 PDO

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20636502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:21:52  来源:igfitidea点击:

mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

phpmysql

提问by brianc

An error occurred in script 'C:\xampp\htdocs\framework\connect.php' on line 13: 
mysql_connect(): The mysql extension is deprecated and will be removed in the future: use       mysqli or PDO instead 
Date/Time: 12-17-2013 15:10:43 

how can i solved this problems. i have look around this maybe the php version problem but still can not delete this error message.

我该如何解决这个问题。我环顾四周,这可能是 php 版本问题,但仍然无法删除此错误消息。

<?php

/* Database config */

$db_host        = 'localhost';
$db_user        = '~';
$db_pass        = '~';
$db_database    = 'banners'; 

/* End config */


$link = @mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB     connection');

mysql_set_charset('utf8');
mysql_select_db($db_database,$link);

?>

回答by TheCarver

Simply put, you need to rewrite all of your database connections and queries.

简而言之,您需要重写所有数据库连接和查询。

You are using mysql_*functions which are now deprecated and will be removed from PHP in the future. So you need to start using MySQLi or PDO instead, just as the error notice warned you.

您正在使用mysql_*现在已弃用的函数,将来会从 PHP 中删除。所以你需要开始使用 MySQLi 或 PDO,正如错误通知警告你的那样。

A basic example of using PDO (without error handling):

使用 PDO(无错误处理)的基本示例:

<?php
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
$result = $db->exec("INSERT INTO table(firstname, lastname) VAULES('John', 'Doe')");
$insertId = $db->lastInsertId();
?>

A basic example of using MySQLi (without error handling):

使用 MySQLi 的基本示例(无错误处理):

$db = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
$result = $db->query("INSERT INTO table(firstname, lastname) VAULES('John', 'Doe')");

Here's a handy little PDO tutorialto get you started. There are plenty of others, and ones about the PDO alternative, MySQLi.

这是一个方便的小 PDO 教程,可帮助您入门。还有很多其他的,以及关于 PDO 替代方案 MySQLi 的那些

回答by Anil Meena

?php

/* Database config */

$db_host        = 'localhost';
$db_user        = '~';
$db_pass        = '~';
$db_database    = 'banners'; 

/* End config */


$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_database);
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

?>