php - php中函数的变量

标签 php mysql function

您好,如何为此编写 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/

相关文章:

php - 方形通知 android(带 GCM)

mysql - 为什么Mysql在join另一个表B时对表A使用全表扫描?

C++ 函数模板特化声明和模板参数;无与 <> 与 <类型>

php - PHP中的函数超时

javascript - 将函数参数作为数据切换属性传递?

php - 使用ajax和jquery删除div id并从数据库中删除

php - 在php网页上显示60,000条记录的最佳方式

php - 计算 MySQL 数据库中 <a> 链接的点击次数

mysql - 在 ROR 中创建数据库时出错

php 多选下拉菜单