<bdo id='2Mklx'></bdo><ul id='2Mklx'></ul>

      <legend id='2Mklx'><style id='2Mklx'><dir id='2Mklx'><q id='2Mklx'></q></dir></style></legend>
      <tfoot id='2Mklx'></tfoot>
    1. <small id='2Mklx'></small><noframes id='2Mklx'>

      <i id='2Mklx'><tr id='2Mklx'><dt id='2Mklx'><q id='2Mklx'><span id='2Mklx'><b id='2Mklx'><form id='2Mklx'><ins id='2Mklx'></ins><ul id='2Mklx'></ul><sub id='2Mklx'></sub></form><legend id='2Mklx'></legend><bdo id='2Mklx'><pre id='2Mklx'><center id='2Mklx'></center></pre></bdo></b><th id='2Mklx'></th></span></q></dt></tr></i><div id='2Mklx'><tfoot id='2Mklx'></tfoot><dl id='2Mklx'><fieldset id='2Mklx'></fieldset></dl></div>

      PHP PDO - 使用 MySQL 变量

      时间:2023-10-05

        <small id='Q0UeY'></small><noframes id='Q0UeY'>

        <i id='Q0UeY'><tr id='Q0UeY'><dt id='Q0UeY'><q id='Q0UeY'><span id='Q0UeY'><b id='Q0UeY'><form id='Q0UeY'><ins id='Q0UeY'></ins><ul id='Q0UeY'></ul><sub id='Q0UeY'></sub></form><legend id='Q0UeY'></legend><bdo id='Q0UeY'><pre id='Q0UeY'><center id='Q0UeY'></center></pre></bdo></b><th id='Q0UeY'></th></span></q></dt></tr></i><div id='Q0UeY'><tfoot id='Q0UeY'></tfoot><dl id='Q0UeY'><fieldset id='Q0UeY'></fieldset></dl></div>
        • <bdo id='Q0UeY'></bdo><ul id='Q0UeY'></ul>
          <legend id='Q0UeY'><style id='Q0UeY'><dir id='Q0UeY'><q id='Q0UeY'></q></dir></style></legend>

            <tfoot id='Q0UeY'></tfoot>
                  <tbody id='Q0UeY'></tbody>

                本文介绍了PHP PDO - 使用 MySQL 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试使用 PDO 在 PHP 中运行查询.该查询在顶部有一些变量来确定排名,除了在 $sql 中使用 SET @var 时,它返回一个空行集.但是,如果我删除有问题的 SQL,它会返回正常.

                I'm trying to run a query in PHP using PDO. The query has some variables at the top to determine a rank, except the when using the SET @var in the $sql, it returns an empty rowset. If I remove the offending SQL however, it returns fine.

                我不想在脚本中返回 @prev_value、@rank_count 或 @rank_increasing,只返回它在 SELECT 中创建的排名.

                I don't want to return @prev_value, @rank_count or @rank_increasing in my script, only the rank it creates in the SELECT.

                你能告诉我我做错了什么吗?

                Can you let me know what I am doing wrong please?

                谢谢

                    $sql = "
                    SET @prev_value = NULL;
                    SET @rank_count = 0;
                    SET @rank_increasing = 0;
                    SELECT a.*
                         , @rank_increasing := @rank_increasing + 1 AS row_num
                         , CASE
                           WHEN @prev_value = score 
                              THEN @rank_count
                           WHEN @prev_value := score 
                              THEN @rank_count := @rank_increasing
                           END AS rank
                      FROM ( 
                           -- INLINE VIEW --
                           ) a
                    ";
                    try {
                        $sth = $dbh->prepare($sql);
                        $sth->execute(array($var1, $var2));
                        return $sth->fetchAll(PDO::FETCH_ASSOC);
                    } catch (Exception $e) {
                        return $e;
                    }
                

                推荐答案

                在这里找到解决方案:https://stackoverflow.com/a/4685040/1266457

                谢谢:)

                修复:

                // Prepare and execute the variables first
                $sql = "
                SET @prev_value = NULL;
                SET @rank_count = 0;
                SET @rank_increasing = 0;
                ";
                $sth = $dbh->prepare($sql);
                $sth->execute();
                
                // Run the main query
                $sql = "
                SELECT a.*
                     , @rank_increasing := @rank_increasing + 1 AS row_num
                     , CASE
                       WHEN @prev_value = score 
                          THEN @rank_count
                       WHEN @prev_value := score 
                          THEN @rank_count := @rank_increasing
                       END AS rank
                  FROM ( 
                       -- INLINE VIEW --
                       ) a
                "; ...
                

                这篇关于PHP PDO - 使用 MySQL 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:PDO:“无效的参数号"用相同的值替换多个参数 下一篇:PDO:找不到驱动程序 php/mysql

                相关文章

                最新文章

                <i id='SWhLD'><tr id='SWhLD'><dt id='SWhLD'><q id='SWhLD'><span id='SWhLD'><b id='SWhLD'><form id='SWhLD'><ins id='SWhLD'></ins><ul id='SWhLD'></ul><sub id='SWhLD'></sub></form><legend id='SWhLD'></legend><bdo id='SWhLD'><pre id='SWhLD'><center id='SWhLD'></center></pre></bdo></b><th id='SWhLD'></th></span></q></dt></tr></i><div id='SWhLD'><tfoot id='SWhLD'></tfoot><dl id='SWhLD'><fieldset id='SWhLD'></fieldset></dl></div>
                <legend id='SWhLD'><style id='SWhLD'><dir id='SWhLD'><q id='SWhLD'></q></dir></style></legend>
                  <bdo id='SWhLD'></bdo><ul id='SWhLD'></ul>

                1. <small id='SWhLD'></small><noframes id='SWhLD'>

                  1. <tfoot id='SWhLD'></tfoot>