gpt4 book ai didi

php - Mysql数据库连接错误

转载 作者:行者123 更新时间:2023-11-29 18:07:17 25 4
gpt4 key购买 nike

可能听起来很明显,但我的数据库没有连接到我的 phpmyadmin。

我是不是做错了什么?我使用 mamp 作为 php 使用的后端服务器。

 <?php
//
require('database.php');
$connection = mysqli_connect('localhost', 'root','','blurt');
// If the values are posted, insert them into the database.
if (isset($_POST['Register'])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];

$query = "INSERT INTO `user` (username, email, password) VALUES ('$username', '$email', '$password')";
//echo $query;
$result=mysqli_query($connection,$query);
if($result){
$smsg = "User Created Successfully.";
}else{
$fmsg ="User Registration Failed";
}
}

$connection = mysqli_connect('localhost', 'root', '', 'blurt');
if (!$connection) {
die("Database Connection Failed" . mysqli_error($connection));
} $select_db = mysqli_select_db($connection, 'blurt');
if (!$select_db) {
die("Database Selection Failed" . mysqli_error($connection));
}
?>

最佳答案

通过查看您的问题,您似乎正在查询后注册连接..

请尝试更改顺序..

在使用之前必须先创建$connection..

有些像下面这样:

// Step -1
$connection = mysqli_connect('localhost', 'root', '', 'blurt');
if (!$connection) {
die("Database Connection Failed" . mysqli_error($connection));
} $select_db = mysqli_select_db($connection, 'blurt');
if (!$select_db) {
die("Database Selection Failed" . mysqli_error($connection));
}

//Step-2
$query = "INSERT INTO `user` (username, email, password) VALUES ('$username', '$email', '$password')";
//echo $query;
$result=mysqli_query($connection,$query);
if($result){
$smsg = "User Created Successfully.";
}else{
$fmsg ="User Registration Failed";
}

关于php - Mysql数据库连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47727144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com