-1

I have a question on executing query inside IF statement. I will start with how i want the process to run. Initially i have an excel file that i want to upload into MYSql database. Using PHPExcel, i am able to get the data from the excel file. Now, what i want to do is for each data inside the excel file, it will compare with existing data inside database. If the data is matched, then it will execute the query to insert into success table, but if the data is not matched, it will execute query to insert into fail table.

Here is my code, the code now just skip the IF condition and execute the fail condition.

$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(),

                )
            );

        }
 }
} 

Any help is appreciated. Thanks

Nik Shakirin
  • 1
  • 1
  • 5
  • 1. Should not $query1 be "SELECT 1 FROM part WHERE partno = :partno"?; 2. You should not check if $statement1 equal to 1, you should check it the statement's results (fetch) equal to 1; 3. I'd use load data infile into temp table from CSV file (can be saved from excel or pre build using php from your xls file) and then move it into stock/fail using SP. It would be much faster then executing multiple queries. – fifonik Feb 19 '19 at 01:08

1 Answers1

0

$statement1 is a 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) {
Nick
  • 138,499
  • 22
  • 57
  • 95