Deprecated: mysql_connect()

前端 未结 15 1107
醉酒成梦
醉酒成梦 2020-11-22 06:55

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

The MySQL code is showing me a message in PHP:

Deprecated: mysql_connect(): T

相关标签:
15条回答
  • 2020-11-22 07:49

    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

    0 讨论(0)
  • 2020-11-22 07:51

    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?

    0 讨论(0)
  • 2020-11-22 07:51
    <?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.

    0 讨论(0)
  • 2020-11-22 07:53

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

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

    0 讨论(0)
  • 2020-11-22 07:54

    Adding a @ works for me!

    I tested with error_reporting(E_ALL ^ E_DEPRECATED);

    0 讨论(0)
  • 2020-11-22 07:56

    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');
    ?> 
    
    0 讨论(0)
提交回复
热议问题