Error while connecting to Database on hosted server

做~自己de王妃 提交于 2019-12-13 11:19:39

问题


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...!!"; 
     }
?>

回答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:

  • http://php.net/manual/en/function.error-reporting.php
  • http://php.net/manual/en/mysqli.error.php
  • http://php.net/manual/en/function.mysqli-connect.php
  • http://php.net/manual/en/language.constants.php

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




回答2:


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);


来源:https://stackoverflow.com/questions/32253577/error-while-connecting-to-database-on-hosted-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!