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

后端 未结 8 812
一向
一向 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: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);
    

提交回复
热议问题