gpt4 book ai didi

php - 选择返回空数组的sql变量

转载 作者:行者123 更新时间:2023-11-29 16:03:51 24 4
gpt4 key购买 nike

我正在尝试在 MySQLSET 3 个变量并获取其中两个变量的总和。

前两个变量,@cFollow@cComment应该分别返回一个整数值(返回的行数);第三个是这两个整数之和。

这是我的SQL:

SET @cFollow = (SELECT COUNT(*) FROM followers WHERE unix > :unix AND following = :user);
SET @cComment = (SELECT COUNT(*) FROM comments WHERE comment_unix > :unix AND comment_track IN (SELECT upload_id FROM uploads WHERE upload_artist = :user));

SET @total = @cFollow + @cComment;

SELECT @total;

当我在 PHPMyAdmin 上测试它时,它返回了正确的值并且工作得很好。但是,当我在 PHP 中测试它时,它返回一个空数组。

这是我的PHP:

$holdPoint = (int)Input::get("hold_point");

$_SQL = "
SET @cFollow = (SELECT COUNT(*) FROM followers WHERE unix > :unix AND following = :user);
SET @cComment = (SELECT COUNT(*) FROM comments WHERE comment_unix > :unix AND comment_track IN (SELECT upload_id FROM uploads WHERE upload_artist = :user));

SET @total = @cFollow + @cComment;

SELECT @total;";
$_PARAMS = [":unix" => $holdPoint, ":user" => $user_id];

$check = DB::getInstance()->queryPro($_SQL, $_PARAMS);
var_dump($check);

这是 var_dump 的结果:

array(0){} // not very impressive...
// should be something like int(1) instead

我整夜都在搜索,学习如何从 MySQL 查询返回 PHP 中的变量,这就是我所得到的。

感谢所有帮助,
干杯。

最佳答案

这个答案并不是真正的答案,而是更多的评论。
另请注意您的查询

SET @cFollow = (SELECT COUNT(*) FROM followers WHERE unix > :unix AND following = :user);
SET @cComment = (SELECT COUNT(*) FROM comments WHERE comment_unix > :unix AND comment_track IN (SELECT upload_id FROM uploads WHERE upload_artist = :user));

SET @total = @cFollow + @cComment;

SELECT @total;

很可能被重写为一个查询

SELECT 
SUM(alias.c) AS total
FROM (
SELECT COUNT(*) AS c FROM followers WHERE unix > :unix AND following = :user
UNION ALL
SELECT COUNT(*) AS c FROM comments WHERE comment_unix > :unix AND comment_track IN (SELECT upload_id FROM uploads WHERE upload_artist = :user)
) AS alias

关于php - 选择返回空数组的sql变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55925262/

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