-3

Warning: mysql_connect(): (HY000/2002): Connection refused in /home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php on line 7

Warning: mysql_select_db(): No such file or directory in /home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php on line 8

Warning: mysql_select_db(): A link to the server could not be established in /home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php on line 8

I have the below code

<?php 
     $localhost="localhost"; 
     $username=b31_16461744; 
     $pass=test123; 
     $dbname=b31_16461744_user; 
     $a= mysqli_connect($localhost,$user,$pass); 
     mysql_select_db($dbname); 
     if($a)
     { 
       echo "connected..";
     } 
     else 
     { 
       echo "not...!!"; 
     }
?>
Gunaseelan
  • 2,494
  • 5
  • 36
  • 43
SUM
  • 23
  • 1
  • 10

2 Answers2

1

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

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Well Ralph. Are you running training courses for beginners now – RiggsFolly Aug 28 '15 at 12:38
  • @RiggsFolly I thought Sam was doing that? I guess I've inherited the torch Smokey. – Funk Forty Niner Aug 28 '15 at 12:39
  • Thanks @Fred-ii- your recommended links helped me alot...thanks once again – SUM Aug 28 '15 at 19:06
  • @Fred-ii- Ok..just the last thing .. i am retrieving data from database & it has started an infinite loop & displaying the data all the way... ??? what is it...?? – SUM Aug 28 '15 at 19:21
  • @SUM it would be best if you were to post a new question along with the code you are now using. Please do not modify your existing question. I will see what I or someone else can do to help. – Funk Forty Niner Aug 28 '15 at 19:30
  • @Fred-ii- i am not allowed to ask more questions for the time being & iam there is a new issue i want to discuss....what should i do now??? – SUM Aug 29 '15 at 14:39
0

I Think Credentials are not correctly set. See Your connection statement. For Reference : While Working On Localhost, We write connection statement as :

$con=mysql_connect("localhost","root","");
$db1=mysql_select_db("DatabaseName",$con);

But, While working on server, we need to change the following credential. Username and password values are must.

    $con=mysql_connect("localhost","Username","password");
    $db1=mysql_select_db("DatabaseName",$con);
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • So you are still using the deprecated **`mysql_`** extension. I hope you realise thay PHP7 does not come with a `mysql_` database extension. You should probably consider this in the future, and for the future of the code you write. – RiggsFolly Aug 28 '15 at 12:40