<?php
$host = 'localhost';
$user = 'root'
$password = '';
$db ='members';
$connection = mysqli_connect("localhost", "user", "password") or die("Unable to connect to the server!");
mysqli_select_db("members", $connection) or die("Couldn't connect to the database!");
我已经安装了xampp并创建了名为&#34; members&#34;的数据库。我试图将它连接到phpmyadmin,但没有工作。我尝试谷歌所有答案,因为三天,但徒劳无功。请帮帮我......
答案 0 :(得分:2)
<?php
$host = 'localhost';
$user = 'root';
$password = '';
$db ='members';
$connection = mysqli_connect($host,$user,$password,$db);// you can select db separately as you did already
if($connection){
// do all your stuff that you want
}else{
echo "db connection error because of".mysqli_connect_error();
}
答案 1 :(得分:1)
您的用户名和密码凭据是否正确?
默认情况下,localhost的用户名= root,密码为空。
此外,问题是什么?是否显示&#34;无法连接到服务器!&#34;?
答案 2 :(得分:1)
$user = 'root'
之后您丢失了一个分号,并且您使用的是mysql_
和mysqli_
的混合。此外,您可以通过将第四个参数传递给mysqli_connect()
$host = 'localhost';
$user = 'root';
$password = '';
$db ='members';
$connection = mysqli_connect($host,$user,$password,$db);// you can select db separately as you did already
if($connection){ echo "Connected Successfully";}else{ echo "Error connecting: . mysqli_connect_error()"; }
使用mysqli_
进行查询:
mysqli_query($connection, "INSERT INTO user_login (uname,upassword,email) VALUES ('$uname','$upassword','$email')");
我建议您使用预准备语句来避免SQL注入。
所以上面的查询看起来像是:
$stmt->prepare("INSERT INTO user_login (uname,upassword,email) VALUES (?,?,?)");
$stmt->bind_param('sss', $uname, $upassword, $email);
$stmt->execute();