简体   繁体   English

如何在 PHP 中连接到 MySQL 数据库

[英]How to connect to MySQL database in PHP

I'm kinda new in PHP, but I was told to create a login form that connects to Mysql database.我是 PHP 新手,但有人告诉我要创建一个连接到 Mysql 数据库的登录表单。

I tried the following code to connect to the database, tried it with, and without the database.我尝试使用以下代码连接到数据库,尝试使用和不使用数据库。 I tried mysql_connect and something else, but always got the same error: (hidden:servername and others are the proper information of the server, just it is not pulbic.)我尝试了 mysql_connect 和其他东西,但总是得到同样的错误:(隐藏:服务器名和其他人是服务器的正确信息,只是它不是公开的。)

Fatal error: Uncaught Error: Call to undefined function mysqli() in C:\\Users\\vg141fy\\login\\login.php:25 Stack trace: #0 {main} thrown in C:\\Users\\vg141fy\\login\\login.php on line 25致命错误:未捕获错误:调用 C:\\Users\\vg141fy\\login\\login.php:25 中未定义的函数 mysqli() 堆栈跟踪:#0 {main} 抛出在 C:\\Users\\vg141fy\\login\\login.php在第 25 行

<?php
    if (ISSET($_POST["submit"]))
    {
        $servername = "hidden:servername";
        $username = "hidden:username";
        $password = "hidden:password";
        $database = "hidden:database";

        $conn= mysqli($servername, $username, $password, $database);
    }
?>

I open a localhost to open my php pages with "php -S localhost:4000", but i don't use XAMPP.我打开一个本地主机以使用“php -S localhost:4000”打开我的 php 页面,但我不使用 XAMPP。 Should I use it, or is there something else that is miss?我应该使用它,还是还有其他遗漏的地方?

Look at the quick start guide .查看 快速入门指南

If you are using the OO interface, you need to include the new keyword.如果使用 OO 接口,则需要包含new关键字。

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

If you are using the procedural interface, then you need to use the mysqli_connect function.如果您使用的是过程接口,那么您需要使用mysqli_connect函数。

$conn = mysqli_connect($servername, $username, $password, $database);

You have to check first that connection successfully established or not您必须先检查连接是否成功建立
use this to connect your database using this用它来连接你的数据库

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

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

?>

if it give success message then error should be in your after connection code so try to create table and check如果它给出成功消息,那么错误应该在你的连接代码中,所以尝试创建表并检查

Try to change database credential below and ensure that it matches yours.尝试更改下面的数据库凭据并确保它与您的匹配。

If the connection failed it will print error below could not connect to database .如果连接失败,它会在下面打印错误无法连接到数据库

  <?php
     $dbhost = 'localhost';
     $dbuser = 'your-db-username';
     $dbpass = 'your-db-pass';
     $conn = mysqli_connect($dbhost, $dbuser, $dbpass);

     if(! $conn ){
        //die('Could not connect: ' . mysqli_error());
    echo 'Could not connect to database ';
     }

     echo 'Connected successfully';
     mysqli_close($conn);
  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM