微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 变量在函数内部未定义,无法在函数内部达到MySQL $连接

我有一个代码,当我在页面上使用它时,但我试图使这个功能.我无法让它工作,似乎变量$customer和$system没有被发送到代码.即使我在Raw中键入它.有什么想法错了吗? $Customer是客户的名称,$system可以是’Source’或’Target’.

function status_total($customer, $system){
        $sql_customer       = "SELECT * FROM `Customer` WHERE Cust_Name = '$customer' LIMIT 0,1";
        $customer_selection = MysqLi_query($conn,$sql_customer);
        $customer_row       = MysqLi_fetch_assoc($customer_selection);
        $env_lines          = $customer_row["Env_Lines"];
        $cust_id            = $customer_row["Cust_ID"];
        $sql_last_records   = "SELECT * FROM $system WHERE Cust_ID = $cust_id ORDER BY Time DESC LIMIT $env_lines";
        $record_selection   = MysqLi_query($conn, $sql_last_records);               

        $result = MysqLi_fetch_all($record_selection, MysqLI_ASSOC);
        $states = array_column($result, "Stat");

        if($states == array_fill(0, count($states), "Run")) {
            echo "Success";
        } else 
            echo "Fail";

    }

https://gist.github.com/R2D2-05/78d81566e4bf0eafd1fa

解决方法:

您的代码的问题是$conn变量,它被视为函数内的局部变量.你应该:

function status_total($customer, $system){
        global $conn;

        $sql_customer       = "SELECT * FROM `Customer` WHERE Cust_Name = '$customer' LIMIT 0,1";
        $customer_selection = MysqLi_query($conn,$sql_customer);
        $customer_row       = MysqLi_fetch_assoc($customer_selection);
        $env_lines          = $customer_row["Env_Lines"];
        $cust_id            = $customer_row["Cust_ID"];
        $sql_last_records   = "SELECT * FROM $system WHERE Cust_ID = $cust_id ORDER BY Time DESC LIMIT $env_lines";
        $record_selection   = MysqLi_query($conn, $sql_last_records);               

        $result = MysqLi_fetch_all($record_selection, MysqLI_ASSOC);
        $states = array_column($result, "Stat");

        if($states == array_fill(0, count($states), "Run")) {
            echo "Success";
        } else 
            echo "Fail";

    }

或者您也可以通过函数传递$conn,因此将函数的定义更改为:

function status_total($conn, $customer, $system){...}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐