dongzhiyong8577 2016-03-31 17:49
浏览 29

怪异的数据库响应

I'm doing simply php query to database and it responds with weird object, I can't find what did I asked for here. I know how to code in js but I'm new in PHP.

Response: http://imgur.com/rLeeoLI

Here's the code:

index.php

<!DOCTYPE html>
<html>

    <head>
        <script type='text/javascript'>
            function spytajBazy(request)
            {
                var wynik = $.ajax({
                    type: "post",
                    url: "ajax_requests.php",
                    data: {request: request},
                    success: function (result) {
                        return result;
                    }
                });

        var array = $.map(wynik, function(value, index) {
             return [value];
        });
                return array;
            }
        </script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
        <title>Admin check</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <script type='text/javascript'>
        var wynik = spytajBazy("sprawdz_ile_grup");
        var array = jQuery.makeArray(wynik);
        console.log(wynik);
        </script>

Functions.php

<?php

class Functions
{
    private $db;

    public function __construct(Database $db)
    {
        $this->db = $db;
    }

    public function sprawdz_ile_grup()
    {
        $query = "SELECT * FROM `groups`";
        $result = $this->db->dataQuery($query);
        return $result->fetchAll();
    }

    public function insert_something()
    {
        $query = "INSERT INTO  `myDB`.`groups` (`id` ,`nazwa` ,`data_utworzenia`)VALUES ('5',  'randomsecond',  '2016-05-18');";
        $result = $this->db->dataQuery($query);
        return $true;
    }
}

Database.php

<?php

class Database
{
    private $conn = null;

    public function __construct()
    {
        try {
            $username = "usrnm";
            $password = "pass";
            $servername = "server";
            $dbname = "dbname";
            $this->conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

        } catch (PDOException $e) {
            trigger_error("Error: " . $e->getMessage());
        }
    }

    public function dataQuery($query, $params = array())
    {
        try {
            $stmt = $this->conn->prepare($query);
            $stmt->execute($params);
            return $stmt;

        } catch (PDOException $e) {
            trigger_error("Error: " . $e->getMessage());
        };
    }
}

ajax_requests.php

<?php
if (!empty($_POST)) {

    $method = $_POST['request'];

    include 'Database.php';
    include 'Functions.php';

    $db = new Database();
    $functions = new Functions($db);

    if (method_exists($functions, $method)) {
        $data = $functions->$method();
        header('Content-Type: application/json');
        echo json_encode($data);
    }
}
?>
  • 写回答

1条回答 默认 最新

  • dougaicha5258 2016-03-31 18:02
    关注

    There's not really a way in which your php code could return javascript functions like that. I think the error is in your javascript code.

    var wynik = $.ajax({ //...
    

    Why are you setting the wynik variable there? I think the console log that you're seeing is the $.ajax function. I bet it works if you change that line to this:

    $.ajax({ //...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数