php - How to connect to the Database?
问题描述
I tried connecting the database in order to create chat by using:
I would like to know what do I need to change in this code in order to connect it to db:
<?php
include 'dbh.php';
$uname= $_POST['username'];
$email=$_POST['email'];
$pass=$_POST['password'];
$sql="insert into signup(username,email,password)
values ('$username','$email','$password')";
$result=$conn->query($sql);
header("Location:index.php");
?>
Thank you.
解决方案
试试这个代码:
$conn = new mysqli($servername, $username, $password, $dbName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
return $conn;
推荐阅读
- mongodb - 为什么MongoDB支持事务?
- node.js - 节点js,当包含“include”和“where:array []”选项时,使用偏移量和限制对findAndCountAll进行Sequelize不起作用
- image - 有没有办法创建在点击或单击时翻转到另一侧(另一页)的图像?
- google-api - Google Calendar API 删除 410 错误是什么意思
- reactjs - 无法编译 React Native 应用程序
- r - 是否有任何函数可以让我将此列表转换为一组向量?
- java - 需要显式类路径的 Gradle 插件和不需要的 Gradle 插件有什么区别?
- php - 如何使用 Symfony4 更新迁移 AppBundle.php
- graphql - ...GatsbySanityImageFluid - 无法读取 null 的属性“流体”
- python - 在 Python 描述符中使用 setattr() 和 getattr()