What are some of the safest ways to connect to a database with PHP?

This question already has an answer here:

  • Why shouldn't I use mysql_* functions in PHP? 14 answers

  • Use PDO functions.

    Database Connection Using PDO:

    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    

    safest ways to connect to a database with PHP

    If you are looking for the safe methods you actually need to fire proof your queries from injections. MySQL extension is going to be deprecated soon, it does not means its not safe now, its just the case that community dropped further development for the extension.

    You can try both PDO and mysqli for your database queries, both are good.

    Your choice should depends upon your database selection -

    PDO supports around 12 different drivers, while MySQLi supports MySQL only.

    List of PDO drivers available

    CUBRID (PDO)
    MS SQL Server (PDO)
    Firebird/Interbase (PDO)
    IBM (PDO)
    Informix (PDO)
    MySQL (PDO)
    MS SQL Server (PDO)
    Oracle (PDO)
    ODBC and DB2 (PDO)
    PostgreSQL (PDO)
    SQLite (PDO)
    4D (PDO) 
    

    Source - pdo-drivers-in-php

    API support

    PDO and MySQLi both offers object-oriented API , but MySQLi also offers a procedural API .


    Yes, your code is outdated. and mysql_connect, mysql_query etc will be deleted soon.

    There are basically two options to use: (without installing 3rd party applications)

    Take a look at http://php.net/manual/en/book.pdo.php

    OR

    use Mysqli http://php.net/manual/en/book.mysqli.php

    链接地址: http://www.djcxy.com/p/26578.html

    上一篇: connect()在PHP 5.6.5中可用

    下一篇: 用PHP连接数据库的最安全的方法是什么?