关于mysql和mysqli的用法[复制]

This question already has an answer here:

I'm a php beginner. I saw many videos in youtube, where they are using mysql instead of mysqli; My editor is NetBeans and Netbeans warns me the mysql is deprecated.

$conn = new mysqli($servername, $username, $password);

My doubt:
Should i learn mysql format or just skip that?

</div>

Dont learn the mysql_* functions. Even if you stumble accross them in legacy code its not hard to understand them.

As you already said by yourself, mysql_* functions are deprecated (as of PHP 5.5) and even removed (as of PHP 7).

Also, MySQLi is much more secure because it supports prepared statements. If you dont want to use MySQLi, you can also use PDO instead.


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

Prepared Statement: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

Definitely skip it. Using depreciated technologies is never a good idea especially in a production environment. mysqli and pdo are better and much safer solutions. PDO is my personal fav ;). Here is a connection to get you started.

<?php

define("DSN", "mysql:host=localhost;dbname=db_name");
define("USERNAME", "root");
define("PASSWORD", "");

$options = array(PDO::ATTR_PERSISTENT => true);

try{
    $conn = new PDO(DSN, USERNAME, PASSWORD, $options);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    echo "connection successful";

}catch (PDOException $ex){

    echo "A database error occurred ".$ex->getMessage();

}