Moodle 中的查询参数数量不正确 *如何从课程页面获取外部链接

发布于 2025-01-14 00:44:32 字数 1078 浏览 1 评论 0原文

亲爱的 我尝试在 Moodle 中运行以下临时数据库查询,但出现错误,

有什么方法可以修复它,谢谢

*我的目标是从课程页面查找所有外部链接 (URL)

SELECT
        concat('<a target="_new" href=%%WWWROOT%%/course/view.php?id=',
                c.id, '">', c.fullname, '</a>') AS Course
        ,c.shortname,r.name
        ,(
            SELECT CONCAT(u.firstname,' ', u.lastname) AS Teacher
            FROM prefix_role_assignments AS ra
                JOIN prefix_context AS ctx ON ra.contextid = ctx.id
                JOIN prefix_user AS u ON u.id = ra.userid
            WHERE ra.roleid = 3 
            AND ctx.instanceid = c.id 
            LIMIT 1
          ) AS Teacher
        ,concat('<a target="_new" href="%%WWWROOT%%/mod/resource/view.php?id=',
                r.id, '">', r.name, '</a>') AS Resource
FROM prefix_resource AS r
    JOIN prefix_course AS c ON r.course = c.id
WHERE r.reference LIKE 'https://stackoverflow.com/%'    

错误消息:

" Error when executing the query: ERROR: Incorrect number of query 
parameters. Expected 2, got 0"

My Dear
I tried to run the following ad-hoc database query in Moodle but i got it error

there any way to fix it , thanks

*My target to find all the external links (URL) from course page

SELECT
        concat('<a target="_new" href=%%WWWROOT%%/course/view.php?id=',
                c.id, '">', c.fullname, '</a>') AS Course
        ,c.shortname,r.name
        ,(
            SELECT CONCAT(u.firstname,' ', u.lastname) AS Teacher
            FROM prefix_role_assignments AS ra
                JOIN prefix_context AS ctx ON ra.contextid = ctx.id
                JOIN prefix_user AS u ON u.id = ra.userid
            WHERE ra.roleid = 3 
            AND ctx.instanceid = c.id 
            LIMIT 1
          ) AS Teacher
        ,concat('<a target="_new" href="%%WWWROOT%%/mod/resource/view.php?id=',
                r.id, '">', r.name, '</a>') AS Resource
FROM prefix_resource AS r
    JOIN prefix_course AS c ON r.course = c.id
WHERE r.reference LIKE 'https://stackoverflow.com/%'    

Error message :

" Error when executing the query: ERROR: Incorrect number of query 
parameters. Expected 2, got 0"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2025-01-21 00:44:32

在Moodle中执行SQL语句时,任何“?”字符用于指示需要随查询一起提供的参数的占位符。

根据您的查询风格,我假设您正在使用 report_customsql 或 block_configurablereports 进行查询,因此您无法选择提供此类参数。

如果您查看此处的文档:https://docs.moodle.org/en/Custom_SQL_queries_report 您将看到解决此问题的方法是替换任何“?”查询中包含“%%Q%%”的字符。

所以固定查询应该如下所示:

SELECT concat('<a target="_new" href="%%WWWROOT%%/course/view.php%%Q%%id=',c.id,'">',c.fullname,'</a>') AS Course,
    c.shortname,r.name, (SELECT CONCAT(u.firstname,' ', u.lastname) AS Teacher
FROM prefix_role_assignments AS ra
JOIN prefix_context AS ctx ON ra.contextid = ctx.id
JOIN prefix_user AS u ON u.id = ra.userid
WHERE ra.roleid = 3 AND ctx.instanceid = c.id LIMIT 1) AS Teacher,
concat('<a target="_new" href="%%WWWROOT%%/mod/resource/view.php%%Q%%id=',r.id,'">',r.name,'</a>') AS Resource
FROM prefix_resource AS r
JOIN prefix_course AS c ON r.course = c.id
WHERE r.reference LIKE 'https://stackoverflow.com/%'    

When executing SQL statements in Moodle, any '?' characters are used to indicate placeholders for parameters that need to be provided along with the query.

From the style of your query, I'm assuming you're using report_customsql or block_configurablereports for your query, so you won't have the option of providing such parameters.

If you look at the documentation here: https://docs.moodle.org/en/Custom_SQL_queries_report you will see that the workaround for this is to replace any '?' characters in your query with '%%Q%%'.

So the fixed query should look like:

SELECT concat('<a target="_new" href="%%WWWROOT%%/course/view.php%%Q%%id=',c.id,'">',c.fullname,'</a>') AS Course,
    c.shortname,r.name, (SELECT CONCAT(u.firstname,' ', u.lastname) AS Teacher
FROM prefix_role_assignments AS ra
JOIN prefix_context AS ctx ON ra.contextid = ctx.id
JOIN prefix_user AS u ON u.id = ra.userid
WHERE ra.roleid = 3 AND ctx.instanceid = c.id LIMIT 1) AS Teacher,
concat('<a target="_new" href="%%WWWROOT%%/mod/resource/view.php%%Q%%id=',r.id,'">',r.name,'</a>') AS Resource
FROM prefix_resource AS r
JOIN prefix_course AS c ON r.course = c.id
WHERE r.reference LIKE 'https://stackoverflow.com/%'    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文