我在这里有以下代码行,我到处都看到如何以这种格式连接到MySQL服务器,但我似乎无法找到如何!
$Conn = mysqli_connect("YourMySQLDetails");
通常,我会这样连接:
$myServer = "localhost";
$myUser = "632496";
$myPass = "xrxy3749";
$myDB = "632496";
但我在网页上收到以下错误
Failed to connect to MySQL: Can't connect to MySQL server on 'YourMySqlDetails' (111)Error:
这是整个文件
<?php
$Conn;
CreateDatabase();
function CreateDatabase(){
$con=mysqli_connect("localhost","632496","xrxy3749","password","632496");
if (mysqli_connect_errno($con))
{
echo "notconnect some Problem Occur";
}
else
{
echo "Connect Successfully";
}
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
AddTables();
}
function AddTables(){
global $Conn;
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="CREATE TABLE servers(ID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ID),ServerName CHAR(60),Url CHAR(100),Players INT, MaxPlayers INT, Uptime INT, LastTimeSeen DATETIME)";
// Execute query
if (mysqli_query($Conn, $sql)){
echo "Table servers created successfully";
}else{
echo "Error creating table: " . mysqli_error();
}
}
?>
答案 0 :(得分:1)
按照这个例子。我认为你的问题很容易解决。我使用这个例子来连接数据库。
<?php
$con=mysqli_connect("localhost","632496","xrxy3749","632496");
if (mysqli_connect_errno($con))
{
echo "notconnect some Problem Occur";
}
else
{
echo "Connect Successfully";
}
?>
答案 1 :(得分:0)
尝试使用此代码连接mysql数据库
$con=mysqli_connect("localhost","632496","xrxy3749","632496");
您已在连接详细信息中添加了额外的密码字段。
答案 2 :(得分:0)
你不能在同一个文件中使用mysql和mysqli这两个函数。
$con=mysqli_connect("localhost","632496","xrxy3749","password","632496");
将其替换为以下代码
$con=mysqli_connect("localhost","632496","xrxy3749","632496");
再犯一次错误
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
将其替换为
$dbhandle = mssqli_connect($myServer, $myUser, $myPass, $dbhandle)
删除此代码
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
替换所有地方
$Conn to $con
答案 3 :(得分:-2)
使用以下代码解决您的问题
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
?>