Mysqli query with a SET variable statment (ie multiple queries)(带有 SET 变量语句的 Mysqli 查询(即多个查询))
问题描述
我想设置一个变量以在 mysqli 查询中使用.这不太行.在 mysqli 之前,我曾经设置查询调用.我玩 db->multi_query($sql) 没有运气.任何人都知道如何使这项工作包括 set 语句?
I want to set a variable to use in a mysqli query. This doesn't quite work. Prior to mysqli I used to set query calls. I played around with db->multi_query($sql) with no luck. Anyone out there have an idea how to make this work including a set statement?
$sql = 'SET @rownum := 0;';
$sql .= 'SELECT @rownum :=@rownum + 1 AS Rank, User_Id, COUNT(User_ID) AS Block_Count
FROM Block_Owners;
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $world, $userId);
// execute the query
$stmt->execute();
推荐答案
在两个单独的查询中进行:
Do it in two separate queries:
$db->query('SET @rownum := 0');
$sql = 'SELECT @rownum :=@rownum + 1 AS Rank, User_Id, COUNT(User_ID) AS Block_Count FROM Block_Owners'
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $world, $userId);
$stmt->execute();
但是请注意,您要运行的查询将始终返回单行(Rank = 1
),因为您使用的是没有 GROUP BY
的聚合函数.
Note, however, that the query you want to run will always return a single row (with Rank = 1
) since you are using an aggregate function without GROUP BY
.
这篇关于带有 SET 变量语句的 Mysqli 查询(即多个查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 SET 变量语句的 Mysqli 查询(即多个查询)
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01