PHP / Mysql - 加载文件和查询记录

时间:2017-01-14 13:39:14

标签: php mysql mysqli

在这里尝试了一些事情,但不知怎的,我似乎无法通过PHP获取我的记录以执行后续操作。基本上我将记录加载到临时表(WORKS),调用proc然后将它们迁移到我的实时表(WORKS),然后我需要简单地循环导入的每个记录以生成图像。如果我回显查询并通过Workbench运行它,我会收回我的数据,但不知何故在PHP中,查询没有返回任何内容,我缺少什么?

public function import_members() {
    $this->filepath=htmlspecialchars(strip_tags($this->filepath)); 

    $status = "success";
    $msg = "";

    $query = "LOAD DATA LOCAL INFILE '" . $this->filepath ."' INTO TABLE temp_imported 
          CHARACTER SET 'latin1'
          FIELDS TERMINATED BY ',' 
          OPTIONALLY ENCLOSED BY '\"' 
          LINES TERMINATED BY '\\n' 
          IGNORE 1 LINES 
          (fname,lname,email,phone,address,city,province,postal,dob,member_no)";

          echo $query . "<br><br>";

          // EXECUTE LOAD - ON SUCCESS
          if ($this->conn->query($query)) {
              try {
                  // CALL PROC TO IMPORT DATA INTO LIB_MEMBERS
                  $stmt = $this->conn->query("CALL sp_import_members(@rowcount)");

                  while ($row = mysqli_fetch_array($stmt)) {
                      $records = $row[0];
                  }

                  mysqli_free_result($stmt);
                  $msg = "File was imported successfully! <br> A total of " . $records . " records were imported from your file" ; 
                } 
                catch (Exception $ex) {
                    $msg = $ex ;
                    $status = 'danger';
                }

               echo $status . "<br><br>";

               if ($status == "success"){ 
                    // GENERATE BARCODE IMAGES FOR IMPORTED
                    $querybar = "Select barcode from lib_members where barcode is not null and DATE(datein) = date(now())";
                    echo $querybar . "<br><br>";

                    $stmt2 = $this->conn->query($querybar);
                    $rownum = $stmt2->num_rows;

                    if ($rownum){
                        echo $rownum;
                    }
                    else {
                        echo "NO";
                    }
                }
            }
            else {
                $msg = "There was a problem importing your file";
            }

            echo "<div style='width:40%; margin:0 auto; text-align:center; font-size:12px;' class='alert alert-". $status ."' role='alert'>" . $msg . "</div>";
        }
 }

我的输出目前显示:

LOAD DATA LOCAL INFILE '/Users/DB/Sites/Scan_MASTER/imported/members.csv' INTO TABLE temp_imported CHARACTER SET 'latin1' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (fname,lname,email,phone,address,city,province,postal,dob,member_no)

success

Select barcode from lib_members where barcode is not null and DATE(datein) = date(now())

Notice: Trying to get property of non-object in /Users/db/Sites/Scan_MASTER/_members.php on line 590

NO

第590行由我的&#34; num_rows&#34;线

2 个答案:

答案 0 :(得分:0)

commit

行之后尝试$this->conn->query($query)

答案 1 :(得分:0)

通过对我的proc调用使用prepare和execute语句而不是使用&#34;查询&#34;来管理找到解决问题的方法。两种情况下的方法。可能不是很漂亮或理想的修复,但它确实有效。