您好,如何为此编写 php 代码,例如我有包含数据列表的文件
AA123_jane_G-1
AA456_oshi_K-3
AA789_pritt_P-4
我使用
读取我的文件function myfile(){
while (($data = fgetcsv($handle, 1000, "_")) !== FALSE) {
..................
........
.......
//get $student_id, $name, $class,
}
我需要使用文件中的student_id获取用户名和密码
function getusernameandpassword($student_id){
........
.......
$result = mysql_query("SELECT student_id, username, password FROM account WHERE student_id= '".$student_id."'");
........
//get $username and $password from other remote server
....... }
执行完这两个函数后,我想收集该学生的所有信息并插入到数据库
function insertDB(){
........
.......
mysql_query("INSERT INTO user_info
(student_id, username, password, name, class) VALUES ($student_id,$username,$password,$name,$class)")
........
.......
//insert all the paramater get from both function for each student to localhost DB}
我的问题是,如何使用 php 在 1 个脚本中编写此函数。如何返回值以及如何获取要输入给其他函数的值
最佳答案
要在函数外部使用函数的变量,您可以返回该变量并将其分配给另一个变量,或者在函数内部将所需变量设置为global
,例如:
function test(){
global $variable;
}
然后您可以在脚本内的任何位置使用它。
关于php - php中函数的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31286650/