3

I have a code that works when I use it on a page but Im trying to make this a function. I cant get it to work, it seems like the variables $customer and $system arent being sent through to the code. Even if I type it in Raw. Any idea whats wrong? $Customer is the name of the customer, $system can be 'Source' or '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

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
R2D2
  • 131
  • 1
  • 2
  • 10
  • 1
    I can't see your $conn and $sql_customer variables defined in the function., it should be the problem. I am not digging into other problems like you are most likely not going to get desired result queried with only Customer name in the case of duplicate customer names and limiting returned data. – smozgur May 22 '15 at 19:19
  • 1
    ... or digging into other glaring problems like **SQL Injection vulnerabilities**, or performance related issues like retrieving *every* column in the table (with `SELECT *`) when we only have a need for two of the columns. – spencer7593 May 22 '15 at 19:22
  • yup that was one of the mistakes @smozgur, thnx. I didnt think I needed to include $conn because I have that in my config.php page thats included in this function.php page – R2D2 May 22 '15 at 19:33
  • 1
    a function is a separate world that cannot read from the outside other than injected objects and variables - unless you globally define the variable, which you should avoid doing that. – smozgur May 22 '15 at 19:37

1 Answers1

5

The problem with your code is $conn variable which is treated a local variable inside a function. You should:

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";

    }

Or you can also pass the $conn through the function, so change the function's definition to:

function status_total($conn, $customer, $system){...}
Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
  • I cant believe I didnt find out that syntax error myself but I guess thats why im still a noob lol. That fixed the first sql problem. And the $conn part solved the rest! thanks, I didnt think it needed that because in my functions.php page the config.php page with database connection is included. – R2D2 May 22 '15 at 19:31
  • 1
    In PHP, by default, functions are scope-bound. Inside a function, you only access variables which are either passed as params of that function or defined in that function. To access outside $vars, you must declare them as `global`; – Mostafa Talebi May 22 '15 at 19:34