【问题标题】:how to return value of selected id in database php如何在数据库php中返回所选ID的值
【发布时间】:2014-05-28 10:06:21
【问题描述】:

我在数据库中插入了一个 csv 文件。我将如何返回 id 并使用它插入另一个表。它总是显示数组到字符串的转换错误。 “返回”有什么问题吗

这是我的控制器

public function uploadThree(){
    $file = $_FILES['file']['tmp_name'];
    $handle = fopen($file,"r");
    while(($fileop = fgetcsv($handle,1000,",")) !==false)
    {
        $appname = $fileop[0];
        $servname = $fileop[1];
        $ciname = $fileop[2];
        $servid = $this->some_model->insertBulkServ($servname); //i tried to get the value here then insert below
        $appid = $this->some_model->insertBulkSingleApp($appname);//i tried to get the value here then insert below
        $this->some_model->insertBulkCI($ciname);
        $this->some_model->ASMAP($appid,$servid);
    }

    if($success == TRUE)
        redirect(base_url().'some_controller/uploadPage');
}

型号

public function insertBulkServ($service) {
/* Inserts csv file for a service */
    $service = $this->db->escape_str($service);

    $queryStr = "Select service from appwarehouse.service where service = '$service' and VISIBILITY = 'VISIBLE'";
    $query = $this->db->query($queryStr);
    if($query->num_rows()>0){

        $queryStr = "SELECT id FROM appwarehouse.service WHERE service='$service' AND visibility = 'VISIBLE';";
        $query = $this->db->query($queryStr);
        $row = $query->result();
        return $row;
        //in here how do i get the ID how do i return it
    }else{
        $queryStr = "INSERT INTO appwarehouse.service(service) VALUES ('$service');";
        $query = $this->db->query($queryStr);

        $queryStr = "SELECT id FROM appwarehouse.service WHERE service='$service' AND visibility = 'VISIBLE';";
        $query = $this->db->query($queryStr);
        $row = $query->result();
        return $row;
    } 
}



public function insertBulkSingleApp($app_name) {
    /* Inserts csv file for an application       */
    $app_name = $this->db->escape_str($app_name);

    $queryStr = "Select * from appwarehouse.application_table where app_name = '$app_name' and VISIBILITY = 'VISIBLE'";
    $query = $this->db->query($queryStr);
    if($query->num_rows()>0){
        $queryStr = "SELECT id FROM appwarehouse.application_table WHERE app_name='$app_name' AND visibility = 'VISIBLE';";
        $query = $this->db->query($queryStr);
        $row = $query->result();
        return $row;
    }
    else{
        $queryStr = "INSERT INTO appwarehouse.application_table(app_name) 
                    VALUES ('$app_name');";
        $query = $this->db->query($queryStr);
        $queryStr = "SELECT id FROM appwarehouse.application_table WHERE app_name='$app_name' AND visibility = 'VISIBLE';";
        $query = $this->db->query($queryStr);
        $row = $query->result();
        return $row;
    } 
}



public function ASMAP($appid,$servid) {

    $appid = $this->db->escape_str($appid);
    $servid = $this->db->escape_str($servid);

    $queryStr = "Select * from appwarehouse.app_service where app_id = '$appid' AND serv_id = '$servid' and VISIBILITY = 'VISIBLE'";
    $query = $this->db->query($queryStr);
    if($query->num_rows()>0){
        return true;
    }
    else{
        $queryStr = "INSERT INTO appwarehouse.app_service(app_id,serv_id) 
                    VALUES ('$appid','$servid');";
        $query = $this->db->query($queryStr);
        return true; 
    } 
}

【问题讨论】:

    标签: php sql codeigniter


    【解决方案1】:

    你可能想要的是:

    $this->db->insert_id()

    执行数据库插入时的插入ID号。

    更多信息:http://ellislab.com/codeigniter/user-guide/database/helpers.html

    【讨论】:

      【解决方案2】:

      你需要做的很简单 查询插入后

      $queryStr = "INSERT INTO appwarehouse.service(service) VALUES ('$service');";
      

      用这个代替选择

      return $this->db->insert_id();
      

      或者如果你真的需要返回对象

      $queryStr = "SELECT id FROM appwarehouse.application_table WHERE app_name='$app_name' AND visibility = 'VISIBLE';";
      $query = $this->db->query($queryStr);
      

      返回 id 而不是对象

      return $query->row()->id;
      

      还有一点需要注意。 insert_id 是最后插入的 id,因此您不必运行 select 查询来获取 id。

      也可以使用row() 选择单个记录。 result() 选择多条记录,因此您将获得一个数组。见here

      【讨论】:

      • 它说未定义的 $id 作为回报 $query->result()->id;
      • 抱歉应该是row() 而不是result()
      • 如果我想用不同的组合插入相同的条目怎么办
      • return $query->row()->id 有问题;当我用不同的组合插入相同的值时
      • insertBulkServ 中,您只插入一条记录并返回ID,然后您将该ID 用于insertBulkSingleApp 中的另一个组合,这样就可以了,您不必担心。
      猜你喜欢
      • 2013-03-06
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2013-10-17
      • 2019-11-14
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      相关资源
      最近更新 更多