试图在xampp中连接sql数据库

时间:2016-03-13 06:33:02

标签: php mysql

<?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,但没有工作。我尝试谷歌所有答案,因为三天,但徒劳无功。请帮帮我......

3 个答案:

答案 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();