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

后端 未结 8 667
一向
一向 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 10:59

    mysql_ functions have been removed from PHP 7. You can now use MySQLi or PDO.

    MySQLi example:

    mysqli_connect($mysql_hostname, $mysql_username, $mysql_password, $mysql_dbname);
    

    mysqli_connect reference link

    0 讨论(0)
  • 2020-11-22 11:00

    For mysqli you can use :

    $db = ADONewConnection('mysqli');

    ... ...

    $db->execute("set names 'utf8'");

    0 讨论(0)
  • 2020-11-22 11:05

    mysql_* functions have been removed in PHP 7.

    You now have two alternatives: MySQLi and PDO.

    The following is a before (-) and after (+) comparison of a migration to the MySQLi alternative, taken straight out of working code:

    -if (!$dbLink = mysql_connect($dbHost, $dbUser, $dbPass))
    +if (!$dbLink = mysqli_connect($dbHost, $dbUser, $dbPass))
    
    -if (!mysql_select_db($dbName, $dbLink))
    +if (!mysqli_select_db($dbLink, $dbName))
    
    -if (!$result = mysql_query($query, $dbLink)) {
    +if (!$result = mysqli_query($dbLink, $query)) {
    
    -if (mysql_num_rows($result) > 0) {
    +if (mysqli_num_rows($result) > 0) {
    
    -while ($row = mysql_fetch_array( $result, MYSQL_ASSOC )) {
    +while ($row = mysqli_fetch_array( $result, MYSQLI_ASSOC )) {
    
    -mysql_close($dbLink);
    +mysqli_close($dbLink);
    
    0 讨论(0)
  • 2020-11-22 11:07

    mysql_* functions have been removed in PHP 7.

    You probably have PHP 7 in XAMPP. You now have two alternatives: MySQLi and PDO.

    Additionally, here is a nice wiki page about PDO.

    0 讨论(0)
  • 2020-11-22 11:10

    You can use mysqli_connect($mysql_hostname , $mysql_username) instead of mysql_connect($mysql_hostname , $mysql_username).

    mysql_* functions were removed as of PHP 7. You now have two alternatives: MySQLi and PDO.

    0 讨论(0)
  • 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

    <?php
    // mysqli
    $mysqli = new mysqli("example.com", "username", "password", "database");
    $result = $mysqli->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?

    0 讨论(0)
提交回复
热议问题