sql连接错误000webhost

时间:2017-03-29 13:43:42

标签: php sql database mysqli database-connection

我使用000 webhost来存储sql数据库。 每次我想连接到它时,我都会收到此错误消息

Warning: mysqli::__construct(): (HY000/2002): Connection refused in /storage/h10/804/1186804/public_html/Handy_Help_PHP_files/conexiune.php on line 7
Connection failed: Connection refused

这是我用来连接的php

<?php
$servername = "localhost:3306";
$username = "id1186804_admin";
$password = "12345";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

有人可以帮我找错吗?

2 个答案:

答案 0 :(得分:0)

使用mysqli也需要添加数据库名称

<?php
$servername = "localhost:3306";
$username = "id1186804_admin";
$password = "12345";

// Create connection
$conn = new mysqli($servername, $username, $password,$yourdbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

这样:

<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

答案 1 :(得分:0)

从host参数中删除端口并按此顺序输入:

mysqli_connect(主机,用户名,密码,则dbname,端口下,插座);