【问题标题】:PHP Function - what am I missing?PHP 函数 - 我错过了什么?
【发布时间】:2011-10-05 09:59:31
【问题描述】:

已解决 - 用户错误 - 如果一个人声明了一个变量,不是吗...

帮助,

我有以下 PHP 函数代码:

function new_respondent() {
    global $link;
    $proc = mysqli_prepare($link, "INSERT INTO trespondent_bps (code) VALUES (uuid());");
    mysqli_stmt_execute($proc);
    $respondent_id = mysqli_insert_id($link);
    mysqli_stmt_fetch($proc);
    mysqli_stmt_close($proc);
    mysqli_clean_connection($link);
    return($code);
}

对我来说,将 UUID 添加到数据库中一点问题都没有 - 但它没有返回它,所以我可以在代码中使用它 - 我错过了什么/做错了什么!!!

提前致谢,

荷马。

【问题讨论】:

  • 我认为 id 字段设置为 AUTO INCREMENT? $code 也没有设置任何东西,如果你想让 func 返回 id,它不应该是 return($respondent_id) 吗?
  • 是的 - 如下 - 多么重要!

标签: php function return


【解决方案1】:

您返回一个未定义的变量$code

function new_respondent() {
    global $link;
    $proc = mysqli_prepare($link, "INSERT INTO trespondent_bps (code) VALUES (uuid());");
    mysqli_stmt_execute($proc);
    $respondent_id = mysqli_insert_id($link);
    mysqli_stmt_fetch($proc);
    mysqli_stmt_close($proc);
    mysqli_clean_connection($link);
    return($respondent_id);
}

【讨论】:

    【解决方案2】:

    试试这个

    function new_respondent() {
        global $link;
        $code = uniqid();
    
        $proc = mysqli_prepare($link, "INSERT INTO trespondent_bps (code) VALUES (" . $code .");");
        mysqli_stmt_execute($proc);
        $respondent_id = mysqli_insert_id($link);
        mysqli_stmt_fetch($proc);
        mysqli_stmt_close($proc);
        mysqli_clean_connection($link);
        return($code);
    }
    

    请注意,我没有逃脱它,因为它是安全的

    【讨论】:

      【解决方案3】:

      你忘了mysqli_stmt_bind_result($code);

          function new_respondent() {
              global $link;
              $proc = mysqli_prepare($link, "INSERT INTO trespondent_bps (code) VALUES (uuid());");
              mysqli_stmt_execute($proc);
              $respondent_id = mysqli_insert_id($link);
      mysqli_stmt_bind_result($code);
              mysqli_stmt_fetch($proc);
              mysqli_stmt_close($proc);
              mysqli_clean_connection($link);
              return($code);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-13
        • 2015-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-31
        • 1970-01-01
        相关资源
        最近更新 更多