douyin7416 2014-12-14 22:34
浏览 59
已采纳

将$ conn变量传递给函数时遇到问题

I am trying to set up a basic shopping cart application with a link to a database. Obviously I have the connection set up at the top of the code with:

        $dsn = "mysql:dbname=MYDATABASE";
        $username = "root";
        $password = "MYPASSWORD";
        try {
            global $conn;
            $conn = new PDO( $dsn, $username, $password );
            $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        } catch ( PDOException $e ) {
          echo "Connection failed: " . $e->getMessage();
        }

And then I have my first step, which asks for various information, and then would save it to a database. The way that I am doing this is as follows:

            function processStep1() {
            $_SESSION["firstname"] = $_POST["firstname"];
            $_SESSION["lastname"] = $_POST["lastname"];
            $_SESSION["address"] = $_POST["address"];
            $_SESSION["city"] = $_POST["city"];
            $_SESSION["state"] = $_POST["state"];
            $_SESSION["zip"] = $_POST["zip"];


            $sql = "INSERT INTO customers VALUES ( '" . $_SESSION["firstname"] . "', '" . $_SESSION["lastname"] . "', '" . $_SESSION["address"] . "', '" . $_SESSION["state"] . "', '" . $_SESSION["city"] . "', '" . $_SESSION["zip"] . "' );";

            try {
              $conn->query( $sql );
            } catch ( PDOException $e ) {
              echo "Query failed: " . $e->getMessage();
            }

            displayStep2();
        }

When I run this page, however, I get the error:

"Notice: Undefined variable: conn" as well as "Fatal error: Call to a member function query() on a non-object".

I did manage to read online that this is because $conn is not being passed to the function, but I am not sure how I would go about passing it, because my method for calling the function processStep1() is as follows:

        if ( isset( $_POST["step"] ) and $_POST["step"] >= 1 and $_POST["step"] <= 5 ) {
          call_user_func( "processStep" . (int)$_POST["step"] );
        } else {
          displayStep1();
        }

So as to facilitate a multi-page system in a streamlined fashion, it simply checks for the step number whenever the form is submitted and then assembles the function call using call_user_func, so how would I pass $conn into that? I'm not certain.

Any help would be appreciated!

  • 写回答

1条回答 默认 最新

  • dongyou5068 2014-12-14 22:43
    关注

    Change the function to take $conn as a parameter:

    function procesStep1($conn) {
        ...
    }
    

    and pass the parameter when you call it:

    call_user_func("processStep" . (int)$_POST["step"], $conn);
    

    Also, in modern versions of PHP you don't need to use call_user_func, you can use variable functions:

    $processStep = "processStep" . (int)$_POST["step"];
    $processStep($conn);
    

    If you don't want to change all your functions like this, you can put

    global $conn;
    

    into the functions. It doesn't need to be in the code outside functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)