找到你要的答案

Q:Deprecated: mysql_connect()

Q:反对:mysql_connect()

I am getting this warning, but the program still runs correctly.

The MySQL code is showing me a message in PHP:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\task\media\new\connect.inc.php on line 2

My connect.inc.php page is

<?php
  $connect = mysql_connect('localhost','root','');
  mysql_select_db('dbname');
?>

What does this mean and how can I eliminate the message?

我得到这个警告,但程序仍然正确运行。

MySQL的代码是显示我在PHP留言:

反对:mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\task\media\new\connect.inc.php on line 2

我connect.inc.php页

<?php
  $connect = mysql_connect('localhost','root','');
  mysql_select_db('dbname');
?>

这意味着什么,我如何消除信息?

answer1: 回答1:

There are few solutions for your problem.

The way with MySQLi would be like this:

<?php
$connection = mysqli_connect('localhost', 'username', 'password', 'database');

To run database queries is also simple and nearly identical with the old way:

<?php
// Old way
mysql_query('CREATE TEMPORARY TABLE `table`', $connection);
// New way
mysqli_query($connection, 'CREATE TEMPORARY TABLE `table`');

Turn off all deprecated warnings including them from mysql_*:

<?php
error_reporting(E_ALL ^ E_DEPRECATED);

你的问题很少有解决方法。

MySQLi的方式会是这样的:

<?php
$connection = mysqli_connect('localhost', 'username', 'password', 'database');

运行数据库查询也很简单,几乎与旧方法相同:

<?php
// Old way
mysql_query('CREATE TEMPORARY TABLE `table`', $connection);
// New way
mysqli_query($connection, 'CREATE TEMPORARY TABLE `table`');

关掉所有过时的警告包括他们mysql_ *:

<?php
error_reporting(E_ALL ^ E_DEPRECATED);
answer2: 回答2:

You can remove the warning by adding a '@' before the mysql_connect.

@mysql_connect('localhost','root','');

but as the warning is telling you, use mysqli or PDO since the mysql extension will be removed in the future.

您可以通过添加一个“@”在mysql_connect移除警告。

@mysql_connect('localhost','root','');

但警告是告诉你,使用mysqli或PDO自MySQL扩展在未来将被删除。

answer3: 回答3:

To suppress the deprecation message for this alone (and stay informed of other deprecations in your code) you can prefix the connect with @:

<?php
$connect = @mysql_connect('localhost','root','');
mysql_select_db('dbname');
?> 

抑制贬低消息仅此(和了解代码中的其他反对)你可以前缀的连接”:

<?php
$connect = @mysql_connect('localhost','root','');
mysql_select_db('dbname');
?> 
answer4: 回答4:

Deprecated features in PHP 5.5.x

The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the **MYSQLi or PDO_MySQL extensions.**

Syntax:

<?php
  $connect = mysqli_connect('localhost', 'user', 'password', 'dbname');

Also, replace all mysql_* functions into mysqli_* functions

instead of

<?php
 $connect = mysql_connect('localhost','root','');
  mysql_select_db('dbname');
?> 

过时的功能在PHP 5.5 X。

原来的MySQL扩展现在已经过时,而且会产生e_deprecated错误连接到数据库时。相反,使用*或* pdo_mysql mysqli扩展。

Syntax:

<?php
  $connect = mysqli_connect('localhost', 'user', 'password', 'dbname');

同时,更换所有mysql_ *功能为mysqli_ *功能

而不是

<?php
 $connect = mysql_connect('localhost','root','');
  mysql_select_db('dbname');
?> 
answer5: 回答5:

That is because you are using PHP 5.5 or your webserver would have been upgraded to 5.5.0.

The mysql_* functions has been deprecated as of 5.5.0

Source

That is because you are using PHP 5.5 or your webserver would have been upgraded to 5.5.0.

的mysql_ *功能已经过时的5.5.0

answer6: 回答6:

This warning is displayed because a new extension has appeared. It suppouse that you still can use the old one but in some cases it´s impossible.

I show you how I do the connection with database. You need just change the values of the variables.

My connection file: connection.php

<?php    
 $host='IP or Server Name (usually "localhost") ';
 $user='Database user';
 $password='Database password';
 $db='Database name';

 //PHP 5.4 o earlier (DEPRECATED)
 $con = mysql_connect($host,$user,$password) or exit("Connection Error");
 $connection = mysql_select_db($db, $con);

 //PHP 5.5 (New method)
 $connection =  mysqli_connect($host,$user,$password,$db);
?>

The extension changes too when performing a query.

Query File: "example.php"

<?php
 //First I call for the connection
 require("connection.php");

 // ... Here code if you need do something ...

 $query = "Here the query you are going to perform";

 //QUERY PHP 5.4 o earlier (DEPRECATED)
 $result = mysql_query ($query) or exit("The query could not be performed");

 //QUERY PHP 5.5 (NEW EXTENSION)
 $result = mysqli_query ($query) or exit("The query could not be performed");    
?>

This way is using MySQL Improved Extension, but you can use PDO (PHP Data Objects).

Firt method can be used only with MySQL databases, but PDO can manage different types of databases.

I'm going to put an example but it´s necessary to say that I only use the first one, so please correct me if there is any error.

My PDO connection file: "PDOconnection.php"

<?php
 $hostDb='mysql:host= "Here IP or Server Name";dbname="Database name" ';
 $user='Database user';
 $password='Database password';

 $connection = new PDO($hostDb, $user, $password);
?>

Query File (PDO): "example.php"

<?php
 $query = "Here the query you are going to perform";
 $result=$connection->$query;
?>

To finish just say that of course you can hide the warning but it´s not a good idea because can help you in future save time if an error happens (all of us knows the theory but if you work a lot of hours sometimes... brain is not there ^^ ).

This warning is displayed because a new extension has appeared. It suppouse that you still can use the old one but in some cases it´s impossible.

我告诉你我如何与数据库连接。您只需要更改变量的值。

我connection.php连接文件:

<?php    
 $host='IP or Server Name (usually "localhost") ';
 $user='Database user';
 $password='Database password';
 $db='Database name';

 //PHP 5.4 o earlier (DEPRECATED)
 $con = mysql_connect($host,$user,$password) or exit("Connection Error");
 $connection = mysql_select_db($db, $con);

 //PHP 5.5 (New method)
 $connection =  mysqli_connect($host,$user,$password,$db);
?>

执行查询时,扩展也会发生更改。

查询文件:“例如.php”

<?php
 //First I call for the connection
 require("connection.php");

 // ... Here code if you need do something ...

 $query = "Here the query you are going to perform";

 //QUERY PHP 5.4 o earlier (DEPRECATED)
 $result = mysql_query ($query) or exit("The query could not be performed");

 //QUERY PHP 5.5 (NEW EXTENSION)
 $result = mysqli_query ($query) or exit("The query could not be performed");    
?>

这种方式是使用MySQL的改进扩展,但您可以使用PDO(PHP数据对象)。

第一方法只能使用MySQL数据库,但PDO可以管理不同类型的数据库。

我要把一个例子却´必要说我只使用第一个,所以如果有任何错误,请纠正我。

我的PDO连接文件:“pdoconnection .php”

<?php
 $hostDb='mysql:host= "Here IP or Server Name";dbname="Database name" ';
 $user='Database user';
 $password='Database password';

 $connection = new PDO($hostDb, $user, $password);
?>

查询文件(PDO):“例如.php”

<?php
 $query = "Here the query you are going to perform";
 $result=$connection->$query;
?>

说完就说你当然可以隐藏的警告,但它´的不是一个好主意,因为可以帮助你在未来如果一个错误发生,节省时间(我们都知道的理论,但如果你的工作很多小时,有时…脑子不在那里)。

answer7: 回答7:

mysql_*, is officially deprecated as of PHP v5.5.0 and will be removed in the future.

Use mysqli_* function or pdo

Read Oracle Converting to MySQLi

mysql_ *,是正式弃用在PHP v5.5.0和在未来将被删除。

使用mysqli_ *功能或PDO

读取Oracle转换到MySQLi

php  mysql  function  deprecated