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'.
当我在一个页面上使用它的时候,我有一个代码,但是我想让它成为一个函数。我无法让它工作,似乎$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
https://gist.github.com/R2D2-05/78d81566e4bf0eafd1fa
1 个解决方案
#1
4
The problem with your code is $conn
variable which is treated a local variable inside a function. You should:
代码的问题是$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";
}
Or you can also pass the $conn
through the function, so change the function's definition to:
或者您也可以通过函数传递$conn,因此将函数的定义更改为:
function status_total($conn, $customer, $system){...}
#1
4
The problem with your code is $conn
variable which is treated a local variable inside a function. You should:
代码的问题是$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";
}
Or you can also pass the $conn
through the function, so change the function's definition to:
或者您也可以通过函数传递$conn,因此将函数的定义更改为:
function status_total($conn, $customer, $system){...}