简体   繁体   English

使用PHP连接MySQL数据库

[英]Using PHP to connect to MySQL database

I am trying to create some very basic php code that will let me know If i have successfully connected to my MySQL database.我正在尝试创建一些非常基本的 php 代码,如果我已成功连接到我的 MySQL 数据库,它将告诉我。 Using MySQLi, I have found out that this is the best way of creating a connection:使用 MySQLi,我发现这是创建连接的最佳方式:

$connection = mysqli_connect(host, username, password, dbname);

and then I saw that you can test to see if you are connected by using an if statement like this:然后我看到您可以通过使用这样的 if 语句来测试您是否已连接:

if(!$connection){
    echo "connection failed"
}
else{
    echo "Successful connection"
}

I believe what I am having the most difficult time trying to understand is the parameters for the mysqli_connect function.我相信我最难理解的是 mysqli_connect function 的参数。 I understand that I need the username, password, and database name from MySQL, however I am fluttering at what needs to go where it says "host".我知道我需要来自 MySQL 的用户名、密码和数据库名称,但是我对 go 的“主机”需要什么感到不安。 Is anyone familiar with connecting to a MySQL database that could help me out?有没有人熟悉连接到可以帮助我的 MySQL 数据库?

The database doesn't have to run on the same computer as the PHP application, it can be on a network server.数据库不必与 PHP 应用程序在同一台计算机上运行,它可以在网络服务器上。 The host parameter is the name or IP address of the server. host参数为服务器的名称或IP地址。

If the database is running on the same machine, use "localhost" as the host argument.如果数据库在同一台机器上运行,请使用"localhost"作为host参数。

A server is anything that handle its clients, Host is where a server is "hosted", for example if I have my birthday party tonight at my home then I would say, "I am hosting the party at this address ", Similarly in computer world, the "address" part is the IP of the server服务器是处理其客户端的任何东西,主机是“托管”服务器的地方,例如,如果我今晚在家里举办生日聚会,那么我会说,“我在这个地址举办聚会”,同样在计算机中世界,“地址”部分是服务器的 IP

"host" simply means IP of the mysql server. “主机”仅表示 mysql 服务器的 IP。

MySQL server on the same computer is hosted on IP "127.0.0.1" also named as "localhost" ( by default ), Hopefully this answers your question.同一台计算机上的 MySQL 服务器托管在 IP “127.0.0.1”也称为“localhost”(默认情况下),希望这能回答您的问题。

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

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