2

在我的 PHP apicación 中,我使用 PDO 连接到 MySQL 数据库,但我遇到了以下问题,即错误消息;

Warning: PDO :: __construct () [pdo. - Construct]: [2002]
"No connection Could be made ​​Actively Because the target machine refused it." 
(trying to connect via tcp :/ / localhost: 3306)

Fatal error: Uncaught exception 'Exception' with message 'Connection failed: SQLSTATE [HY000] [2002]

访问数据库的代码是:

    private $db = NULL;

    const DB_SERVER = "localhost";
    const DB_USER = "root";
    const DB_PASSWORD = "usbw";
    const DB_NAME = "status_poster";

  public function __construct() {
    //**below the error here!!**
    $dsn = 'mysql:dbname=' . self::DB_NAME . ';host=' . self::DB_SERVER;//
    //var_dump($dsn);
    try {
        $this->db = new PDO($dsn, self::DB_USER, self::DB_PASSWORD);
    } catch (PDOException $e) {
        throw new Exception('Connection failed: ' . $e->getMessage());
    }

    return $this->db;
    }

我觉得奇怪的是,相同的参数连接到“mysqli”我没有问题,我连接成功,运行CRUD语句没有问题,代码如下;

编码

    function getProducts() {
    $products = array();
    $mysqli = new mysqli("localhost", "root", "usbw", "Datos");
    if (mysqli_connect_errno()) {
        printf("Error in conection: %s\n", mysqli_connect_error());
        exit();
    }
    $query = "SELECT Id,sku,name,price FROM products";
    if ($result = $mysqli->query($query)) {
        while ($obj = mysqli_fetch_object($result)) {
       //instructions...
        }
    }
    $mysqli->close();
    return $products;
    }

好吧,我需要连接 PDO 但我找不到解决方案,等待建议或帮助!

抱歉,在论坛里找不到怎么回复图片,但是是这个;

解决我的问题,将USBWebserver的端口从3307-3306(3306是错误输出的端口)更改。现在工作!

在此处输入图像描述

注意:上面的端口是3307

4

1 回答 1

1

解决我的问题,将USBWebserver的端口从3307-3306(3306是错误输出的端口)更改。现在工作!

于 2013-06-26T20:09:17.773 回答