gpt4 book ai didi

javascript - 奇怪的数据库响应

转载 作者:行者123 更新时间:2023-11-29 21:11:21 25 4
gpt4 key购买 nike

我正在对数据库进行简单的 php 查询,它以奇怪的对象响应,我找不到我在这里要求的内容。我知道如何用 js 编码,但我是 PHP 新手。

回复:http://imgur.com/rLeeoLI

代码如下:

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>

函数.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;
}
}

数据库.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);
}
}
?>

最佳答案

实际上并没有一种方法可以让你的 php 代码返回这样的 javascript 函数。我认为错误出在您的 JavaScript 代码中。

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

为什么要在那里设置 wynik 变量?我认为您看到的控制台日志是 $.ajax 函数。我敢打赌,如果您将该行更改为:

$.ajax({ //...

关于javascript - 奇怪的数据库响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36340286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com