-1

I am trying to create some very basic php code that will let me know If i have successfully connected to my MySQL database. Using MySQLi, I have found out that this is the best way of creating a connection:

$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(!$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. 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". Is anyone familiar with connecting to a MySQL database that could help me out?

ANON
  • 75
  • 1
  • 9
  • `host, username, password, dbname` < those are constants, you do know that right? – Funk Forty Niner Oct 03 '19 at 16:01
  • 1
    *"Is anyone familiar with connecting to a MySQL database that could help me out?"* - It's in the manual https://www.php.net/manual/en/function.mysqli-connect.php – Funk Forty Niner Oct 03 '19 at 16:01
  • @FunkFortyNiner I know that I need to put actually information where those words are, I just am not sure what to put where host is. – ANON Oct 03 '19 at 16:02
  • Hey, read the manual: host is an ip-adress, or localhost or socket path. – freeek Oct 03 '19 at 16:02
  • @FunkFortyNiner That is basically what I have at that link, but where does the "127.0.0.1" where host is come from? – ANON Oct 03 '19 at 16:03
  • 1
    @ANON That's the standard IP address for `localhost`. – Barmar Oct 03 '19 at 16:12
  • Going down the `mysqli` road may be a bad investment if your time. PDO is a much more flexible, more fully featured database layer that can do everything `mysqli` can and then some. It's not MySQL specific, it has named placeholders, and above all else, a much more sensible API. If you're not too committed, it's worth switching. – tadman Oct 03 '19 at 16:34

2 Answers2

2

The database doesn't have to run on the same computer as the PHP application, it can be on a network server. The host parameter is the name or IP address of the server.

If the database is running on the same machine, use "localhost" as the host argument.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Okay so if i have the web server running and the MySQL database open on the same machine then I can simply just put local host and the PHP will know to connect to the MySQL? – ANON Oct 03 '19 at 16:03
  • Unless there's something wrong with the MySQL configuration on the machine. – Barmar Oct 03 '19 at 16:05
  • Okay, so i put my edited code at the end of my question, this is currently just displaying the code into the web browser. Any idea why that is happening? – ANON Oct 03 '19 at 16:11
  • That's a webserver configuration problem. See https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page – Barmar Oct 03 '19 at 16:12
  • You also have `-` instead of `_` in `mysqli_connect`. – Barmar Oct 03 '19 at 16:12
  • yeah i am working from a virtual machine so i cannot copy and paste so i had to retype all of that. the - is an _ in the actual code I have but ill check our your link – ANON Oct 03 '19 at 16:13
1

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

"host" simply means IP of the mysql server.

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.