XAMPP - Fatal error: Call to undefined function mysql_connect()

混江龙づ霸主 提交于 2020-01-15 06:05:41

问题


I downloaded downloading XAMPP for my learning lesson. After I finished the install, I can use it normally. But when I try to connect to MySQL I keep return this error.

<?php

define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PWD","");
define("DB_DBNAME","text");
define("DB_CHARSET","utf8");

?>
function connect(){
    $link=mysql_connect("DB_HOST","DB_USER","DB_PWD") or die("连接失败Error:".mysql_error().":".mysql_error());
    mysql_select_db(DB_DBNAME);
    return $link;
}

回答1:


First, Kindly ensure the MySQL service is running.

  • Open XAMPP Control Panel
  • Click on Start button corresponding to MySQL module.

Then, Try checking to see if the PHP MySQL extension module is being loaded:

<?php
    phpinfo();
?>

Run the above code/page and search for mysql. If it's not there, add the following to the php.ini file:

extension=php_mysql.dll

Update: mysql_* functions have been removed in PHP 7. You probably have a php7 in XAMPP. Please use PDO or mysqli_connect("DB_HOST","DB_USER","DB_PWD") instead of mysql_connect().




回答2:


First of all looking at your code; you have this part:

    function connect(){
        $link=mysql_connect("DB_HOST","DB_USER","DB_PWD") or die("连接失败Error:".mysql_error().":".mysql_error());
        mysql_select_db(DB_DBNAME);
        return $link;
    }

After you have closed the php tag("?>"); meaning what comes after the closing php tag is not part of your php code. So try inserting the closing php tag at the end of your code. Something like this:

<?php

define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PWD","");
define("DB_DBNAME","text");
define("DB_CHARSET","utf8");

function connect(){
    $link=mysql_connect("DB_HOST","DB_USER","DB_PWD") or die("连接失败Error:".mysql_error().":".mysql_error());
    mysql_select_db(DB_DBNAME);
    return $link;
}
?>

As for your code; I would simply rewrite it as:

    <?php
    $DB_HOST = "localhost"; 
    $DB_DBNAME = "text"; 
    $DB_USER = "root"; 
    $DB_PWD = ""; 

    $db_link = mysql_connect($DB_HOST, $DB_USER, $DB_PWD) or die('连接失败Error:'.mysql_error());  
    mysql_select_db($DB_DBNAME, $db_link) or die('连接失败Error:'.mysql_error());
    ?>

Then try connecting to your MySQL database.



来源:https://stackoverflow.com/questions/38124614/xampp-fatal-error-call-to-undefined-function-mysql-connect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!