gpt4 book ai didi

php - 需要有关变量 COUNT 查询的帮助

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

我的模型中有以下功能。我有一个名为 item_like 的表。在该表中,我有两列 - saturn_1 和 like_id。当 saturn_1 等于数组中 'id' => $item->id 的值时,我想获取 like_id 的总数。我对编码很陌生,无法找出问题所在。

感谢您的帮助。

public static function formatCallbackChallenge($item, $template = 'challenges') {
static $extensions = null;
if($extensions === null) {
$extensions = Extensions_Model_Front::getByMethod('item_list');
}

$item_title = (strlen($item->title) > 25) ? substr($item->title, 0, 25) . '...' : $item->title;

$db = JO_Db::getDefaultAdapter();
$total_likes = new JO_Db_Expr('(SELECT COUNT(like_id) FROM item_like WHERE item = $item->id LIMIT 1)');


$data = array(
'template' => $template,
'sql_row' => isset($item->sql_row) ? $item->sql_row : null,,
'id' => $item->id,
'title' => $item_title,
'total_likes' => $total_likes,
);



if($extensions) {
$front = JO_Front::getInstance();
foreach($extensions AS $id => $ext) {
$data_ext = call_user_func(array($front->formatModuleName($ext . '_model_item'), 'listing'), $data, $item);
if($data_ext && is_array($data_ext)) {
$data = array_merge($data, $data_ext);
}
}
}
return $data;

}

最佳答案

我不知道您正在使用哪个数据库,并且我假设查询运行但返回错误的数据...根据您正在查找的内容,我看到两个错误...

  1. PHP 错误,$item->id 将不会在单引号 PHP 内部进行解析。
  2. LIMIT 1 是多余的,可能会导致问题。

将方法内的第 9 行重写为:

$total_likes = new JO_Db_Expr("SELECT COUNT(like_id) FROM item_like WHERE item = {$item->id}");

关于php - 需要有关变量 COUNT 查询的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30250907/

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