Vladut Maican Vladut Maican - 3 months ago 12
PHP Question

How to connect to a hosted database?

I have my databse hosted on a site.
How can I modify this:

$con=mysqli_connect("localhost", "root", "", "amit");


in order to connect to my database hoted on the www.abc.com (example) site?

Answer

For making the username display onto the next page, you would need to store your authenticated user's username in a session, as HTTP is a stateless protocol.

So, after you have successfully authenticated the user, set the session variable with the username.

if($authenticated)
{
   $_SESSION['username'] = $sUserName;
   //Then redirect the user to the account.php page
   header('location:account.php')
}
else {
 //redirect to the login page.
}

Once you've set the session variable you can use the variable anywhere, but don't forget to start the session at every page using, session_start() Then you can access the username on account.php as follows:

if(isset($_SESSION['username']))
{
   echo $_SESSION['username'];
}

Also, answering your second question: Just replace "localhost" with any host of your choice to connect to a remote database engine.

Cheers!

Comments