[英]Variable invisible outside a function
I have written a query to get customer information from my database but say I want to use it on another page. 我已经写了一个查询来从数据库中获取客户信息,但是我想在另一个页面上使用它。 I don't want to have to copy and paste to the other page to use it. 我不想复制并粘贴到其他页面上来使用它。
I have looked at a function but I don't know how to get the variables out of the function. 我看过一个函数,但是我不知道如何从函数中获取变量。
This is my current function: 这是我当前的功能:
function getCustomer($customerid) {
$getcustomer = mysql_query("SELECT * FROM hire_customers WHERE id='".$customerid."'");
$fetch = mysql_fetch_assoc($getcustomer);
$cust_firstname = $fetch['firstname'];
$cust_lastname = $fetch['lastname'];
$cust_address = $fetch['address'];
$cust_town = $fetch['town'];
$cust_postcode = $fetch['postcode'];
$cust_cont1 = $fetch['contact1'];
$cust_number1 = $fetch['contactnumber1'];
$cust_cont2 = $fetch['contact2'];
$cust_number = $fetch['contactnumber2'];
$cust_email = $fetch['email'];
$cust_idform1 = $fetch['idform1'];
$cust_idnfo1 = $fetch['idinfo1'];
$cust_idform2 = $fetch['idform2'];
$cust_idinfo2 = $fetch['idinfo2'];
$cust_enterdby = $fetch['enteredby'];
}
This is my customer page 这是我的客户页面
getCustomer($customerid);
echo $cust_firstname;
but nothing is echoed out. 但没有回音。
Do I need to be looking at a class or object to do this? 我需要查看类或对象来执行此操作吗? Have I gone wrong with my function. 我的功能出错了吗?
What I would like to do is have a PHP file with all my customer functions (update, select, etc) in one place. 我想做的是将一个PHP文件及其所有客户功能(更新,选择等)放在一个位置。
I think you should just return $fetch
and then access it as a variable outside of the function. 我认为您应该只返回$fetch
,然后在函数外部将其作为变量访问。
function getCustomer($customerid) {
$customerid = mysql_real_escape_string($customerid);
$getcustomer = mysql_query("SELECT * FROM hire_customers WHERE id='".$customerid."'");
$fetch = mysql_fetch_assoc($getcustomer);
return $fetch;
}
$data=getCustomer($customerid);
echo data['firstname'];
This should get you started: 这应该使您开始:
function getCustomer($customerid) {
$getcustomer = mysql_query("SELECT * FROM hire_.. etc");
$customer_data = mysql_fetch_assoc($getcustomer);
return $customer_data; // return here
}
$customer = getCustomer($customerid);
$cust_firstname = $customer['firstname'];
您需要从函数中return $variable;
一个值return $variable;
In your code, variable $cust_firstname;
在您的代码中,变量$cust_firstname;
is visible only inside that function. 仅在该函数内部可见。
Please read this: PHP variable scope 请阅读以下内容: PHP变量范围
It works like this to reduce memory usage on web server - when function ends - variable is destroyed and memory freed. 这样做的目的是减少Web服务器上的内存使用-当函数结束时-变量被破坏并释放内存。
Basically - you have to return
that value somehow. 基本上-您必须以某种方式return
该值。 Dave Chen wrote nice answer above/below, you can use his code. Dave Chen在上下都写了一个不错的答案,您可以使用他的代码。
BTW you can use extract(); 顺便说一句,你可以使用extract(); function and reduce your code size and save a lot of time. 功能并减少代码大小并节省大量时间。
It works like this: 它是这样的:
$fetch = mysql_fetch_assoc($getcustomer);
extract($fetch, EXTR_PREFIX_ALL, "cust_"); // creates variables from associative array
echo $cust_firstname; // magic! :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.