使用 PHPExcel 在 FOR 循环内的 IF 语句中执行查询

Execute a query in IF statement inside FOR loop using PHPExcel

我对在 IF 语句中执行查询有疑问。 我将从我希望流程如何 运行 开始。 最初我有一个 excel 文件,我想上传到 MYSql 数据库。 使用 PHPExcel,我能够从 excel 文件中获取数据。 现在,我想要做的是对 excel 文件中的每个数据,它将与数据库中的现有数据进行比较。 如果数据匹配,则执行查询插入成功table,如果数据不匹配,则执行查询插入失败table.

这是我的代码,现在的代码只是跳过 IF 条件并执行失败条件。

$query1 = "SELECT EXISTS(SELECT 1 FROM part WHERE partno = :partno)";
$statement1 = $connect->prepare($query1);

$query2 = "INSERT INTO stock (userid, vendorid, partno, partname) 
VALUES (:userid, :vendorid, :partno, :partname)";
$statement2 = $connect->prepare($query2);

$query3 = "INSERT INTO fail (userid, vendorid, partno, partname) 
VALUES (:userid, :vendorid, :partno, :partname)";
$statement3 = $connect->prepare($query3);





foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
 $highestRow = $worksheet->getHighestRow();
 for($row=2; $row<=$highestRow; $row++)
 {
   $statement1->execute(
       array(
        ':partno'           =>  $worksheet->getCellByColumnAndRow(0, $row)->getValue()
       )
   );
   if ($statement1=="1"){

        $statement2->execute(
            array(
                ':userid'           =>  $_SESSION['user_id'],
                ':vendorid'         =>  $_SESSION['vendorid'],
                ':partno'           =>  $worksheet->getCellByColumnAndRow(0, $row)->getValue(),
                ':partname'         =>  $worksheet->getCellByColumnAndRow(1, $row)->getValue(),

            )
        );

    }
    else{

        $statement3->execute(
                array(
                    ':userid'           =>  $_SESSION['user_id'],
                    ':vendorid'         =>  $_SESSION['vendorid'],
                    ':partno'           =>  $worksheet->getCellByColumnAndRow(0, $row)->getValue(),
                    ':partname'         =>  $worksheet->getCellByColumnAndRow(1, $row)->getValue(),

                )
            );

        }
 }
} 

感谢任何帮助。谢谢

$statement1 是一个 PDOStatement object, and so you can't compare it to "1". What you need to do is fetch the result of the query and compare that to 1. You can do that with fetchColumn:

if ($statement1->fetchColumn() == 1) {