3

I created this custom website which has a login page. There's no wordpress or any CMS involved. It's working fine when I run the website in the local server. I'm not really sure how to host it in a shared server despite of trying many times, and migrate the simple database I have which has 4 columns(index, username, email, password).

Errors recieved:

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: NO) in /home4/ablev3/public_html/connect.php on line 2

Database Connection FailedAccess denied for user 'root'@'localhost' (using password: NO)


Here's the code of my connect.php file:-

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

What should I keep instead of 'localhost'and 'root' when I upload the file to hostgator shared server? Seems like that were im getting the error. Thanks.

Will appreciate any help.


##Attempt 2##

I tried the suggestions in the following order, still unresolved, what am I doing wrong? Please see below:

  1. I made a new database
  2. Created and added a new user and set new password for that database
  3. Linked the user to the database
  4. Changed 'root' to replace with my DB username
  5. Changed '' to my DB password

Now errors I get(when typing the domain name) looks like this:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home4/ablev3/public_html/index.php:1) in /home4/ablev3/public_html/index.php on line 2

My connect.php code, after changes:-

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

My index.php code after changes:-

 <?php 
session_start();
require('connect.php');
//3. If the form is submitted or not.
//3.1 If the form is submitted
if (isset($_POST['username']) and isset($_POST['password'])){
//3.1.1 Assigning posted values to variables.
$username = $_POST['username'];
$password = $_POST['password'];
//3.1.2 Checking the values are existing in the database or not
$query = "SELECT * FROM `tableOTA` WHERE username='$username' and password='$password'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
//3.1.2 If the posted values are equal to the database values, then session will be created for the user.
if ($count == 1){
$_SESSION['username'] = $username;
}else{
//3.1.3 If the login credentials doesn't match, he will be shown with an error message.
echo "Invalid Login Credentials.";
}
}
//3.1.4 if the user is logged in Greets the user with message
if (isset($_SESSION['username'])){
header( 'Location:introduction.php' );
$username = $_SESSION['username'];
echo "Hai " . $username . "";
echo "This is the Members Area";
echo "<a href='logout.php'>Logout</a>";
}else{
//3.2 When the user visits the page first time, simple login form will be displayed.
?>
<!DOCTYPE html>
<html>
 <head>

<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
    <!-- Form for logging in the users -->
  >     <div class="register-form"> 

..............
..........
.........

What am I doing wrong? how to rectify this error?

peterh
  • 11,875
  • 18
  • 85
  • 108
  • 3
    do you really think hostgator would give you superuser-level access to a shared mysql instance? You'll have to update YOUR code to use the credentials/host that they provided, not the other way around. – Marc B Jun 29 '15 at 18:30
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 29 '15 at 18:50
  • Please don't add tags like "UNRESOLVED" to your title. They're noisy and unnecessary. –  Jun 30 '15 at 01:27
  • okay. I'm new here, so.. – Able Varghese Jun 30 '15 at 01:32

3 Answers3

1

May be the Username or Password you are using is in the php code are wrong.

For Hostgator:

The mysql admin username is the username that was supplied to you by hostgator for your hostgator account.

The mysql password can be changed in Plesk under Server>Database Servers (I presume the initial password is the password given to you by hostgator for your hostgator account, but I never logged in before changing my password).

Attempt 2

Move the session_start(); to top of the page always. Before session_start() there should be no code(php/html/anything). And add @ob_start(); before session start.

               <?php
                  @ob_start();
                  session_start();
               ?>
Manohar Gunturu
  • 676
  • 1
  • 10
  • 23
0

First you need to create the database by logging into the Cpanel on the server and transfer the data to the new database. These are the below steps

  1. Log into your cPanel
  2. Click the MySQL Database Wizard under the Databases heading
  3. Next to New Database enter a name for your database and click Next Step
  4. Next to Username enter a username.
  5. Enter a password next to Password, enter it again for Password (Again) and then click Create User
  6. On the next page, you'll assign privileges for the user to the database. Check the box next to All Privileges and then click Next Step.
  7. you have remember these details of the database, user name , and password and add them to you code at

$connection = mysql_connect('localhost', 'User Name', 'Password');

if (!$connection){

    die("Database Connection Failed" . mysql_error());

}

$select_db = mysql_select_db('New Database');
  1. Then open the PHPMYADMIN on the CPanel and transfer all the data to the new database on the server by importing the sql file.
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • I did that @Chandu , it's still unresolved. Please see my update to my question above where I added my code and errors I got after making the change that you suggested. Why am I still getting those errors? What to do from there? – Able Varghese Jun 30 '15 at 01:17
-1

What you need to do is setup Database Users in Cpanel.

Login and look for this icon:

enter image description here

There you can create your users and passwords. once you do that, use it in your configuration files.

CodeGodie
  • 12,116
  • 6
  • 37
  • 66
  • I did that @CodeGodie , it's still unresolved. Please see my update to my question above where I added my code and errors I got after making the change that you suggested. Why am I still getting those errors? What to do from there? – Able Varghese Jun 30 '15 at 01:19
  • @Abie so why the downvote? care to explain? perhaps I can explain better. Were you able to resolve your issue? – CodeGodie Jun 30 '15 at 03:38
  • I didn't down vote you. Someone else did. With my current reputation points(11) I cannot down vote or upvote anyone. If you think this is a relevant question give an upvote to it. Still trying to resolve. Thanks for ur input. – Able Varghese Jun 30 '15 at 20:22