首页 > 解决方案 > How to connect to the Database?

问题描述

I tried connecting the database in order to create chat by using:

https://youtu.be/k8DhWcdKanM

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.

标签: phpmysqlchat

解决方案


试试这个代码:

$conn = new mysqli($servername, $username, $password, $dbName);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

return $conn;

推荐阅读