无法使用PHP / WAMP服务器连接到MySQL数据库

问题描述 投票:-1回答:1

我无法从正在处理的PHP项目连接到我的mysql数据库。这是我的代码:

 function __construct($engine, $server, $db, $user, $password, $debug = false) {        
    switch (strtolower($engine)) {
        case "mysql":
            $this->dsn = "mysql:dbname=" . $db . ";host=" . $server;
            break;
    }
    $this->user= $user;
    $this->password= $password;
    $this->debugMode = $debug;
}

这是我的配置文件:

    <?php

//Configuration MYSQL
define("ENGINE", "mysql");
define("SERVER", "localhost");
define("DB", "movie_guide");
define("USER", "root");
define("PASSWORD", "ROOT");

这不起作用。我对PHP非常陌生,所以我不知道如何调试它:我什至不知道这是否是曾经执行过的代码。我在.js文件中有以下ajax调用:

function getAll() {
console.log("Get all");
$.ajax({
    url: "getAllMovies.php",        
    type: "GET",
    dataType: "json",
    error: reportProblem,
    success: function (data) {
        console.log(data);
        loadMovies(data);
    }
});    

}

将所有内容记录在控制台中,但是既不会报告问题,也不会执行数据(或我在那里输入的任何内容)。我正在使用WAMP服务器和MySQL 8.0(正在运行)。有什么想法吗?

EDIT:一些额外的代码(数据库的实例化和getAllMovies.php中的代码)

数据库说明:

 <?php
require_once("class.Conection.DB.php");
require_once("./configuration/config.php");

$cn = new ConectionDB(ENGINE, SERVER, DB, USER, PASSWORD);

和getAllMovies.php:

    <?php

require_once ("session.php");
require_once("./configuration/connectDB.php");

$cn->connect();
$cn->consult("SELECT * FROM movies");
$datos = $cn->nextRegistry();
$cn->disconnect();
$movieList= array('data' => $data);

header('Content-Type: application/json');
echo json_encode($movieList);

第二编辑:添加我的连接方法:

function connect() {
        try {
            $this->connection= new PDO($this->dsn, $this->user, $this->password);

            if ($this->debugMode) {
                $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
            } else {
                $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            $retorno = true;
        } catch (PDOException $e) {

            $this->connection= null;
            $this->lastError= "{" . date("d/m/Y H:i:s") . "} " . $e->getMessage();
            $return = false;
        }
        return $return;
    }
javascript php mysql database database-connection
1个回答
0
投票

调用新类时使用数据库配置常量

 $blah = new YourClass(ENGINE,SERVER,DB,USER,PASSWORD);
© www.soinside.com 2019 - 2024. All rights reserved.