Brandyn
Brandyn

Reputation: 1017

Website - Connecting to MySQL database

I am aware this is a stupid question, but to connect to a database that is on my local server, do i connect via this code:

mysql_connect("localhost","user","pass")

so using localhost connection. This seems to make sense, as it is sending a message to the host computer to connect to it's local database.

or do i use this piece of code:

mysql_connect("Ipaddress","user","pass")

This also makes sense to me somehow. Which one do i use for my website which i am hosting at home.

EDIT: obviously I want it so that people from all over the world enter information, and it is sent to a database. It isn't meant to be used by me.

Upvotes: 0

Views: 148

Answers (3)

Michael Garrison
Michael Garrison

Reputation: 941

A better way to do this is:

<?php
    $mysqli = new mysqli("localhost", "user", "password", "database");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    };
?> 

This is what I personally use, there are other ways to do this.

Upvotes: 1

Jean-Georges
Jean-Georges

Reputation: 688

Your database will most likely be stored on the same server as your program, you should put localhost. Anyway, take some time to learn about PDO. It's more secure, cleaner and actually easier to use.

Upvotes: 1

wouterdz
wouterdz

Reputation: 141

You should use localhost:

You could something like this:

<?php
$mysql = mysql_connect("localhost", "user", "pass");
if(!mysql){
die('Could not connect: ' . mysql_error());
}

//code her
mysql_close(mysql) //stops the connection
?>

Upvotes: 1

Related Questions