我有一个MySQL数据库,我试图弄清楚如何使用php文件访问它。我一直收到错误,所以我想知道是不是因为我的主机名不正确。如果是这样,任何人都可以帮助我确切地确定它是什么?该数据库托管在本网站上:https://christopherliao2002.000webhostapp.com/。
以下是我的HTML代码。
<?php
//Step1
$db = mysqli_connect('localhost','***********','password','******')
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<h1>PHP connect to MySQL</h1>
<?php
//Variables
$servername = "localhost";
$username = "*****";
$password = "****";
$dbname = "******";
//Connection to database
$conn = new mysqli($servername, $username, $password, $dbname);
//Test connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
</body>
</html>
答案 0 :(得分:0)
尝试不使用try / catch:
//Variables
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
//Connection to database
$conn = new mysqli($servername, $username, $password, $dbname);
//Test connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());}
答案 1 :(得分:0)
试试这种格式
$servername = "localhost"; // This is fixed for 000webhost.
$username = "id***_name";
$password = "****";
$dbname = "id***_name";
答案 2 :(得分:0)
问题在于您的主机名。通常主机名为'localhost',在000webhost中,主机名类似于mysql#.wewewehost.com,'#'需要替换为您需要检查主机提供商成员区域的号码。
请不要公开发布您的用户名和密码
$db = mysqli_connect('mysql#.000webhost.com','xxxxx','password','xxxxxx')
or die('Error connecting to MySQL server.');
?>