Reputation: 69
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;
}
Upvotes: 5
Views: 61049
Reputation: 476
For me the fix was changing from the function call to the class initialization, as mentioned here https://www.w3docs.com/snippets/php/fatal-error-call-to-undefined-function-mysqli-connect.html
From
$link = mysqli_connection($host, $user, $password, $db_name);
$result = mysqli_query($link, $query) or die(mysqli_error($link));
To
$link = new mysqli($host, $user, $password, $db_name);
$result = mysqli_query($link, $query) or die(mysqli_error($link));
Upvotes: 0
Reputation: 31
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.
Upvotes: 1
Reputation: 9974
First, Kindly ensure the MySQL service is running.
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().
Upvotes: 8