[Solved] Error while connecting to Database on hosted server


Sidenote: Assuming the credentials are correct, given to you by your web host.

There are several problems with this code (taken from a comment you left).

Firstly, three of your declarations are not quoted and are being treated as constants.

PHP error reporting would have thrown notices of undefined constants.

These are treated as constants:

 $username=b31_16461744; 
 $pass=test123; 
 $dbname=b31_16461744_user; 

You are also referencing the wrong variable for the username being $user which should be $username. Error reporting would have signabled an undefined variable notice.

Then you’re mixing mysql_ with mysqli_ syntax. Those different MySQL APIs do NOT intermix. You must use the same one throughout your code.

Sidenote: The other question you posted Access denied for user ‘test123’@’192.168.0.38’ (using password: NO) you are using sql306.byethost31.com for the host. Make sure that is correct. I have no idea what settings that host wants you to use.

<?php 
     $localhost="localhost"; 
     $username="b31_16461744"; 
     $pass="test123"; 
     $dbname="b31_16461744_user"; 
     $a= mysqli_connect($localhost, $username, $pass); 
     mysqli_select_db($a, $dbname); 
     if($a)
     { 
       echo "connected..";
     } 
     else 
     { 
       echo "not...!!"; 
     }
?>

or just use all four parameters:

<?php 
     $localhost="localhost"; 
     $username="b31_16461744"; 
     $pass="test123"; 
     $dbname="b31_16461744_user"; 
     $a= mysqli_connect($localhost, $username, $pass, $dbname); 

     if($a)
     { 
       echo "connected..";
     } 
     else 
     { 
       echo "not...!!" . mysqli_error($a); 
     }
?>

However, your else with the echo does not help you. Use mysqli_error() to get the real error.

I.e.: or die("Error " . mysqli_error($a));

Example from the manual

$link = mysqli_connect("myhost","myuser","mypassw","mydb")
        or die("Error " . mysqli_error($link)); 

References:


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production

6

solved Error while connecting to Database on hosted server