Fatal error: Uncaught Error: Call to undefined function mysql_connect()

后端 未结 8 826
一向
一向 2020-11-22 10:41

I am trying to do a simple connection with XAMPP and MySQL server, but whenever I try to enter data or connect to the database, I get this error.

Fata

8条回答
  •  时光说笑
    2020-11-22 11:14

    It is recommended to use either the MySQLi or PDO extensions. It is not recommended to use the old mysql extension for new development, as it was deprecated in PHP 5.5.0 and was removed in PHP 7.

    PHP offers three different APIs to connect to MySQL. Below we show the APIs provided by the mysql, mysqli, and PDO extensions. Each code snippet creates a connection to a MySQL server running on "example.com" using the username "username" and the password "password". And a query is run to greet the user.

    Example #1 Comparing the three MySQL APIs

    query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
    $row = $result->fetch_assoc();
    echo htmlentities($row['_message']);
    
    // PDO
    $pdo = new PDO('mysql:host=example.com;dbname=database', 'username', 'password');
    $statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
    $row = $statement->fetch(PDO::FETCH_ASSOC);
    echo htmlentities($row['_message']);
    
    // mysql
    $c = mysql_connect("example.com", "username", "password");
    mysql_select_db("database");
    $result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
    $row = mysql_fetch_assoc($result);
    echo htmlentities($row['_message']);
    ?>
    

    I suggest you try out both MySQLi and PDO and find out what API design you prefer.

    Read Choosing an API and Why shouldn't I use mysql_* functions in PHP?

提交回复
热议问题