数据库无法连接,未返回任何结果

时间:2017-08-17 11:13:23

标签: javascript php mysql ajax

所以我有三个PHP文件,我试图通过这些文件连接我的数据库。它似乎没有连接,我试图连接它,所以我的javascript文件中的ajax将有希望工作。

BaseClass.php:

<?php
    require("Conn.php");
    require("MySQLDao.php");

    $handle = fopen("php://input", "rb");
    $raw_post_data = '';
    while (!feof($handle)) {
        $raw_post_data .= fread($handle, 8192);
    }
    fclose($handle);

    if (empty($raw_post_data))
    {
        $returnValue["status"] = false;
        $returnValue["title"] = "Error";
        $returnValue["message"] = "No Data Recieved";
        echo json_encode($returnValue);
        return;
    }
    else
    {
        $dao = new MySQLDao();
        if ($dao->openConnection() == false)
        {
            $returnValue["status"] = false;
            $returnValue["title"] = "Error";
            $returnValue["message"] = "Connection Could Not Be Established Between Server And Database";
            echo json_encode($returnValue);
        }
        else
        {
            //Decodes data, dont change
            $body = json_decode($raw_post_data, true);
            $recieved = $body["data"];

            //Gets the result of a query
            //$result = $dao->MySQLDaoMethodName(parameters);

            //Return the result of the query
            echo json_encode($result);
        }
        $dao->closeConnection();
        return;
    }
?>

当我在chrome中运行它时,它显示的是:

{&#34; status&#34;:false,&#34; title&#34;:&#34;错误&#34;,&#34;消息&#34;:&#34;没有收到数据&# 34;}

MySQLDao.php:

<?php
    //Class for holding queries
    class MySQLDao
    {
        var $dbhost = null;
        var $dbuser = null;
        var $dbpass = null;
        var $mysqli = null;
        var $dbname = null;
        var $result = null;


        //constructor
        function __construct()
        {
            $this->dbhost = Conn::$dbhost;
            $this->dbuser = Conn::$dbuser;
            $this->dbpass = Conn::$dbpass;
            $this->dbname = Conn::$dbname;
        }

        //Attempt a connection to the database
        public function openConnection()
        {   

            //Try and connect to the database
            $this->mysqli = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
            //If the connection threw an error, report it
            if (mysqli_connect_errno())
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        //Get method for retrieving the database conection
        public function getConnection()
        {
            return $this->mysqli;
        }

        //Close the connection to the database
        public function closeConnection()
        {
            //If there is a connection to the database then close it
            if ($this->mysqli != null)
                $this->mysqli->close();
        }

        //-----------------------------------QUERY METHODS-------------------------------------

        public function getResults($data)
        {

            $sql = "SELECT room.room_description FROM room WHERE room.room_id = 1";

            $result = $this->mysqli->query($sql);


            //if (mysql_num_rows($result) == 1) {
            //  $obj = mysql_fetch_object($result, 'obResults');

            //}

            echo json_encode($result);

            echo($result);

        }

    }
?>

我在Chrome中运行时没有显示任何内容。即使我将echo语句放在某些函数中。

Conn.php:

<?php
    class Conn
    {
        public static $dbhost = "***";
        public static $dbname = "***";
        public static $dbuser = "***";
        public static $dbpass = "";
    }
?>

我的test.html的一部分:

function callPHP() {
    $.ajax ({

        type: "GET",
        datatype: "application/json",
        url: "MySQLDao.php",
        data: { action : 'getResults()' },
        //error: function(err){console.log(err)},
        success: function(output) {

            console.log(output);

        }
        //error, function(err){console.log(err)}
    });


}

我基本上只是希望能够编写查询方法并将这些查询的结果传输到我的js,这是因为我的javascript中有一些图表,我想从数据库中获取数据。所有这些代码都没有产生我认为的任何错误,但它只是没有返回任何东西。 所有帮助赞赏!谢谢!

0 个答案:

没有答案