gpt4 book ai didi

php - 在 PhpMyAdmin 面板中运行但不在 Php 脚本中运行的 SQL 查询

转载 作者:行者123 更新时间:2023-11-29 02:47:36 27 4
gpt4 key购买 nike

我有一个让我头疼的查询,它在我输入 phpmyadmin 时运行但没有产生任何输出,甚至在运行脚本时也没有产生错误,我不能说它是连接或任何东西,我有另一个文件几乎和这个完全一样,它可以完美地工作但不是这个,至少即使在这一点上出现错误也会受到赞赏,下面是我的脚本:

<?php

require_once 'include/DB_Connect.php';
$db = new Db_Connect();
$conn = $db->connect();




// get all comments table
$result = $conn->prepare("SELECT sale_comments._id ,sale_comments._user_fk,
users._fname, users._lname,
sale_comments._comment,
sale_comments._date_created
FROM sale_comments, users
WHERE sale_comments._user_fk = users._id
ORDER BY sale_comments._date_created DESC ");
$result->execute();

$response["comments"] = array();
$result->bind_result($id, $user_fk, $fname, $lname, $comment, $date_created);


while($row = $result->fetch())
{
$comment = array();
$comment["id"] = $id;
$comment["user_fk"] = $user_fk;
$comment["fname"] =$fname;
$comment["lname"] = $lname;
$comment["comment"] = $comment;
$comment["date_created"] = $date_created;


$response["message"] = "Loaded";
$response["error"] = FALSE;
array_push($response["comments"], $comment);

}

echo json_encode($response);

?>

还有我的其他运行完美的脚本

<?php


require_once 'include/DB_Connect.php';
$db = new Db_Connect();
$conn = $db->connect();




// get all products from products table
$result = $conn->prepare("SELECT _id, _user_fk, _title, _description, _price, _currency, _category, _location, _image, _date_created FROM sales ORDER BY _date_created DESC");
$result->execute();

$response["sales"] = array();
$result->bind_result($id, $user_fk, $title, $description, $price, $currency, $category, $location, $image, $date_created);


while($row = $result->fetch())
{
$sale = array();
$sale["id"] = $id;
$sale["user_fk"] = $user_fk;
$sale["title"] = $title;
$sale["description"] =$description;
$sale["price"] = $price;
$sale["currency"] = $currency;
$sale["category"] = $category;
$sale["location"] = $location;
$sale["image"] = $image;
$sale["date_created"] = $date_created;


$response["message"] = "Loaded";
array_push($response["sales"], $sale);

}

echo json_encode($response);



?>

Image showing both my files are in the same folder

Image showing the same query running in phpmyadmin

Image showing the file not displaying anything when ran in browser

最佳答案

终于弄明白了,我应该说这是一个非常愚蠢的错误,代码没问题,我的意思是逻辑,问题是我的变量命名,

我在绑定(bind)中将 $comment 声明为一个字段

$result->bind_result($id, $user_fk, $fname, $lname, $comment, $date_created);

然后我继续错误地将它声明为要托管查询生成的数据的数组,在一天结束时它自己消耗了 :D 感谢大家的回答,下次我会格外小心

$comment = array();
$comment["id"] = $id;
$comment["user_fk"] = $user_fk;
$comment["fname"] =$fname;
$comment["lname"] = $lname;
$comment["comment"] = $comment;
$comment["date_created"] = $date_created;




$result->bind_result($id, $user_fk, $fname, $lname, $comment, $date_created);
while($row = $result->fetch())
{
$commentArray = array();
$commentArray["id"] = $id;
$commentArray["user_fk"] = $user_fk;
$commentArray["fname"] =$fname;
$commentArray["lname"] = $lname;
$commentArray["comment"] = $comment;
$commentArray["date_created"] = $date_created;


$response["message"] = "Loaded";
$response["error"] = FALSE;
array_push($response["comments"], $commentArray);

}`

关于php - 在 PhpMyAdmin 面板中运行但不在 Php 脚本中运行的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40085622/

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