当我在我的php文件中添加mysqli_connect
语句时,它会停止处理整个脚本。如果使这个语句注释我的PHP文件按预期工作。我将此文件添加到wordpress网站。
我也尝试使用mysql_connect()进行连接,但结果是一样的。
我的代码:
<?php
require_once ($_SERVER['DOCUMENT_ROOT'].'/wp-config.php');
ini_set('display_errors', 1);
error_reporting(E_ALL);
//phpinfo();
echo DB_NAME ."<br>";
echo DB_USER ."<br>";
echo DB_PASSWORD ."<br>";
echo DB_HOST."<br>"
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);
// database port is also 3306 which is default
if (!$conn) {
echo "Connection failed: " . mysqli_connect_error();
}
echo "Connected successfully";
?>
php.info()
已安装mysqli的输出也已启用。
我是否需要包含一些文件来访问mysqli_connect()
功能。
如何连接数据库?
答案 0 :(得分:1)
你错过了 echo DB_HOST的分号。&#34;
require_once ($_SERVER['DOCUMENT_ROOT'].'/wp-config.php');
ini_set('display_errors', 1);
error_reporting(E_ALL);
//phpinfo();
echo DB_NAME ."<br>";
echo DB_USER ."<br>";
echo DB_PASSWORD ."<br>";
echo DB_HOST."<br>";
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);
// database port is also 3306 which is default
if (!$conn) {
echo "Connection failed: " . mysqli_connect_error();
}
echo "Connected successfully";
?>
答案 1 :(得分:0)
使用此代码。您在DB_HOST."<br>"
行末尾错过了半冒号。代码中的每件事都很好。
<?php
require_once ($_SERVER['DOCUMENT_ROOT'].'/wp-config.php');
ini_set('display_errors', 1);
error_reporting(E_ALL);
//phpinfo();
echo DB_NAME ."<br>";
echo DB_USER ."<br>";
echo DB_PASSWORD ."<br>";
echo DB_HOST."<br>";
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);
// database port is also 3306 which is default
if (!$conn) {
echo "Connection failed: " . mysqli_connect_error();
}
echo "Connected successfully";
?>