【问题标题】:How to connect to MySQL db (xampp) using php?如何使用 php 连接到 MySQL db (xampp)?
【发布时间】:2017-05-29 08:07:21
【问题描述】:

我正在尝试连接到 phpstorm 中的 mysql db,但我没有成功。 感谢您的帮助。

<?php
$servername = "http://localhost:8012";
$username = "root";
$password = "";

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

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

错误是:502 Bad Gateway

【问题讨论】:

  • 请添加您的代码。
  • connect_error 的错误是什么?还可以尝试将您的 $servernamevalue 更改为 localhost。你的 IDE 绝对不是问题
  • 502 错误网关
  • 可能是因为您指定了 HTTP URL 而不是服务器名称。
  • 确认你已经正确安装了 xampp 或者你正在使用的任何东西,并且 Apache 和 mysql 一样运行

标签: php mysql xampp


【解决方案1】:

你可以这样连接数据库:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "";
$port = "8012";

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

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

【讨论】:

  • 502 网关错误
  • 加载资源失败:服务器响应状态为 502 (Bad Gateway)
【解决方案2】:

<?php
$servername = "localhost:8012";
$username = "root";
$password = "";
$database = "dbname";

// Create connection
$conn = mysqli_connect($servername, $username, $password,$database) or die(mysqli_error($conn);

echo "Connection Successful";

【讨论】:

    猜你喜欢
    • 2019-06-29
    • 1970-01-01
    • 2013-08-30
    • 2020-04-23
    • 2012-09-18
    • 2015-01-22
    • 1970-01-01
    • 2017-01-27
    • 2014-08-07
    相关资源
    最近更新 更多