programing

더 이상 사용되지 않음 : mysql_connect ()

nasanasas 2020. 9. 3. 19:29
반응형

더 이상 사용되지 않음 : mysql_connect ()


이 경고가 표시되지만 프로그램은 여전히 ​​올바르게 실행됩니다.

MySQL 코드는 PHP에서 메시지를 표시합니다.

더 이상 사용되지 않음 : mysql_connect () : mysql 확장은 더 이상 사용되지 않으며 향후 제거 될 예정입니다. 2 행의 C : \ xampp \ htdocs \ task \ media \ new \ connect.inc.php 대신 mysqli 또는 PDO를 사용하십시오.

connect.inc.php페이지는

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

이것은 무엇을 의미하며 어떻게 메시지를 제거 할 수 있습니까?


문제에 대한 몇 가지 해결책이 있습니다.

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);

교체해야하는 정확한 파일과 줄 위치는 "/System/Startup.php> line : 2"입니다. error_reporting (E_All); error_reporting (E_ALL ^ ​​E_DEPRECATED);


mysql_connect 앞에 '@'를 추가하여 경고를 제거 할 수 있습니다.

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

그러나 경고에서 알 수 있듯이 mysql 확장은 향후 제거 될 예정이므로 mysqli 또는 PDO를 사용하십시오.


이것만으로도 사용 중단 메시지를 억제하고 코드에서 다른 사용 중단에 대한 정보를 유지하려면 연결 앞에 @를 붙일 수 있습니다.

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

PHP 5.5.x에서 더 이상 사용되지 않는 기능

원래 MySQL의 확장은 이제 사용되지 않으며 생성 E_DEPRECATED데이터베이스에 연결할 때 오류를. 대신 ** MYSQLi 또는 PDO_MySQL 확장을 사용하십시오 . **

통사론:

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

또한 모든 mysql_*기능을 mysqli_*기능 으로 대체하십시오.

대신에

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

이 경고는 새 확장이 나타났기 때문에 표시됩니다. 당신이 여전히 오래된 것을 사용할 수 있다고 가정하지만 어떤 경우에는 불가능합니다.

데이터베이스와 연결하는 방법을 보여줍니다. 변수 값만 변경하면됩니다.

내 연결 파일 : 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);
?>

쿼리를 수행 할 때 확장자도 변경됩니다.

쿼리 파일 : "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");    
?>

이 방법은 MySQL Improved Extension 을 사용하지만 PDO (PHP Data Objects)를 사용할 수 있습니다 .

첫 번째 방법은 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);
?>

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 ^^ ).


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

enter image description here

Source


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


Its just a warning that is telling you to start using newer methods of connecting to your db such as pdo objects

http://code.tutsplus.com/tutorials/php-database-access-are-you-doing-it-correctly--net-25338

The manual is here

http://www.php.net/manual/en/book.pdo.php


Warning "deprecated" in general means that you are trying to use function that is outdated. It doeasnt mean thaqt your code wont work, but you should consider refactoring.

In your case functons mysql_ are deprecated. If you want to know more about that here is good explanation already : Why shouldn't I use mysql_* functions in PHP?


<?php 
$link = mysqli_connect('localhost','root',''); 
if (!$link) { 
    die('Could not connect to MySQL: ' . mysqli_error()); 
} 
echo 'Connection OK'; mysqli_close($link); 
?>

This will solve your problem.


PDO class replaces these methods. Example for Mysql or MariaDB :

$BDD_SQL = new PDO('mysql:host='.BDD_SQL_SERVER.';dbname='.BDD_SQL_BASE.';charset=utf8', 
        BDD_SQL_LOGIN, BDD_SQL_PWD, 
        array(
          PDO::ATTR_EMULATE_PREPARES => false,
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //launch exception if error
          PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC
                ));

Source : PDO Class


If you have done your coding then

ini_set("error_reporting", E_ALL & ~E_DEPRECATED); 

is good option but if you are in beginning then definitely you should use mysqli.


Well, i just faced such message today when i moved to new hosting! anyway i have tried to change the "mySQL" to "mySQLi" but not working, so i have done this:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
# Turn off all error reporting
error_reporting(0);
$connect_myconn = "Database Connection";
$hostname_myconn = "localhost";
$database_myconn = "db name";
$username_myconn = "user name";
$password_myconn = "pass";
$myconn = mysql_connect($hostname_myconn, $username_myconn, $password_myconn) or die("<h1 style=margin:0;>A MySQL error has occurred.</h1><p><b>Your Query:</b> " . $connect_myconn . "<br /> <b>Error Number:</b> (" . mysql_errno() . ")</p>" . mysql_error());
mysql_select_db($database_myconn, $myconn);
?>

The trick is to set error reporting off :)

# Turn off all error reporting
error_reporting(0);

For PHP 7+ you can use this code instead:

ini_set('display_errors', 0);
ini_set('log_errors', 1);

Thanks


put this in your php page.

ini_set("error_reporting", E_ALL & ~E_DEPRECATED); 

Adding a @ works for me!

I tested with error_reporting(E_ALL ^ E_DEPRECATED);

참고URL : https://stackoverflow.com/questions/21797118/deprecated-mysql-connect

반응형