0

Hi I am making a login/register database. I am not hosting the database on the same server so how do I make it point to my separate domain? Also if you can tell me what the root is pointing to... Thx

Here are my files:

db.php

<?php
$connection = mysql_connect('localhost', 'root', '');
if (!$connection){
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('register');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
?>

Links (my scripts and original scripts): https://docs.google.com/document/d/1u60X_mKd9z548qrh_VjdyTx3hziiZiYPLUa_rJX11mQ/edit?usp=sharing

2
  • 1
    change localhost to the server's domain or IP. You should look into using mysqli or pdo if just starting out. Commented Apr 22, 2016 at 1:16
  • Simply amend the mysql_connect parameters to match your database server's host name, username and password. IP address should work fine, but also machine name or DNS entry, depending on your network setup. Commented Apr 22, 2016 at 3:14

2 Answers 2

1

For starters, DON'T USE mysql_connect()! Use either mysqli or PDO. Second, don't have your application log in to MySQL as root; create an application-specific unprivileged user (with a password!) for this purpose.

You'll need to create your unprivileged user with connect privilege from the web server's IP or DNS address, and instead of connecting to localhost your PHP will need to connect to the DB server's IP or DNS address, like so:

$dsn = "mysql:host=mysqlserver.mynetwork.com;dbname=register";
try
{
    $conn = new PDO($dsn, $appuser, $apppasswd);
}
catch (PDOException $e)
{
    echo 'Connection failed: ' . $e->getMessage();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could try the following example

$server = "192.168.1.1"; //server name or IP Address 
$username = "root"; //database username
$password = "password"; //database password
$database = "mydatabase";   //database to connect to

$connection = mysqli_connect( $server, $username, $password, $database);

if (!$connection){
   die("Database Connection Failed" . mysqli_error());
}

else{

//sql statement

}

mysqli_close($connection);

Thanks

1 Comment

As @chris85 mention you should look at using mysqli. I have updated my answer to reflect this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.