【问题标题】:Cannot connect to MySQL db, PHP无法连接到 MySQL 数据库,PHP
【发布时间】:2017-11-13 12:24:00
【问题描述】:

您好,我正在尝试使用 mircloud 主机服务连接 MySQL 数据库,但无法连接。我尝试使用 phpMyAdmin 在本地连接,但仍然给我这个错误。这是我的连接代码。

<!-- connot connect to database -->
<?php
define (DB_USER, "root");
define (DB_PASSWORD, "gOJTkc7QSL");
define (DB_DATABASE, "rbkLastTask");
define (DB_HOST, "https://node42861-env-2276018.mircloud.host");
?>

<?php  
$server = DB_HOST;
$user = DB_USER;
$pw = DB_PASSWORD;
$db = DB_DATABASE;
// $con = mysqli_connect($server,$user,$pw,$db);
$con=mysqli_connect($server,$user,$pw,$db);
Echo mysqli_connect_error();
if($con)
{
    echo "success";
}
else
{
    echo "failed";
}
?>

【问题讨论】:

  • 错误信息/代码是什么?
  • 粘贴你的代码和错误
  • 您的代码和错误丢失
  • 请分享有问题的代码
  • 这是错误信息:php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not knownfailed

标签: php mysql phpmyadmin


【解决方案1】:

试试这个代码

这样写你的代码

<?php 
define ("DB_USER", "root"); //use "" for DB_USER
define ("DB_PASSWORD", "");//use "" for DB_PASSWORD
define("DB_DATABASE", "test");//use "" for DB_DATABASE
define ("DB_HOST", "localhost");  //use "" for DB_HOST

$server = DB_HOST; $user = DB_USER; $pw = DB_PASSWORD; $db = DB_DATABASE; // $con = mysqli_connect($server,$user,$pw,$db);
$con=mysqli_connect($server,$user,$pw,$db) or die (mysqli_connect_error()); //remove ; and echo and add or for die()
if($con) { echo "success"; } else { echo "failed"; } ?>

【讨论】:

  • 对不起,但它给了我这个错误:用户'root'@'localhost'的访问被拒绝(使用密码:否)
  • 你可以用你的用户名密码试试
  • 这在我看来是对的,但添加解释会大大提高您的回答质量。你做了什么,为什么你认为它会有所帮助? 解释您的答案,以便所有技能水平的未来访问者都可以从中学习。 Code-only answers are considered low quality on SO
猜你喜欢
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 2014-08-26
  • 2012-12-29
  • 1970-01-01
  • 2016-08-21
  • 2016-11-13
相关资源
最近更新 更多