src/MysqliLib.php
private static function generateBatchQueryCore(array $rows, $tableName, \mysqli $mysqli)
    {
        $firstRow = true;
        $dataStringRows = array(); # will hold an array list of strings like "('x', 'y', 'z')"
        
        if (count($rows) == 0)
        {
            throw new \Exception("Cannot create batch query with no data.");
        }
        
        foreach ($rows as $row)
        {
            if ($firstRow)
            {
                $columns = array_keys($row);
                sort($columns);
                $firstRow = false;
            }
            
            ksort($row);
            $escapedRow = self::escapeValues($row, $mysqli);
            
            $quotedValues = array();
            # Need just the values, but order is very important.
            foreach ($escapedRow as $columnName => $value)
            {
                if ($value !== null)
                {
                    $quotedValues[] = "'" . $value . "'";
                }
                else
                {
                    $quotedValues[] = 'NULL';  
                }
            }
            
            $dataStringRows[] = "(" . implode(",", $quotedValues) . ")";
        }
        
        $columns = ArrayLib::wrapElements($columns, '`');
        $query = "INTO `" . $tableName . "` (" . implode(',', $columns) . ") " .
                 "VALUES " . implode(",", $dataStringRows);
        
        return $query;
    }
private static function generateBatchQueryCore(array $rows, $tableName, \mysqli $mysqli)
    {
        $firstRow = true;
        $dataStringRows = array(); # will hold an array list of strings like "('x', 'y', 'z')"
        
        if (count($rows) == 0)
        {
            throw new \Exception("Cannot create batch query with no data.");
        }
        
        foreach ($rows as $row)
        {
            if ($firstRow)
            {
                $columns = array_keys($row);
                sort($columns);
                $firstRow = false;
            }
            
            ksort($row);
            $escapedRow = self::escapeValues($row, $mysqli);
            
            $quotedValues = array();
            # Need just the values, but order is very important.
            foreach ($escapedRow as $columnName => $value)
            {
                if ($value !== null)
                {
                    $quotedValues[] = "'" . $value . "'";
                }
                else
                {
                    $quotedValues[] = 'NULL';  
                }
            }
            
            $dataStringRows[] = "(" . implode(",", $quotedValues) . ")";
        }
        
        $columns = ArrayLib::wrapElements($columns, '`');
        $query = "INTO `" . $tableName . "` (" . implode(',', $columns) . ") " .
                 "VALUES " . implode(",", $dataStringRows);
        
        return $query;
    }
private static function generateBatchQueryCore ( array $ rows , $ tableName , \ mysqli $ mysqli ) { $ firstRow = true ; $ dataStringRows = array ( ) ; # will hold an array list of strings like "('x', 'y', 'z')" if ( count ( $ rows ) == 0 ) { throw new \ Exception ( "Cannot create batch query with no data." ) ; } foreach ( $ rows as $ row ) { if ( $ firstRow ) { $ columns = array_keys ( $ row ) ; sort ( $ columns ) ; $ firstRow = false ; } ksort ( $ row ) ; $ escapedRow = self :: escapeValues ( $ row , $ mysqli ) ; $ quotedValues = array ( ) ; # Need just the values, but order is very important. foreach ( $ escapedRow as $ columnName => $ value ) { if ( $ value !== null ) { $ quotedValues [ ] = "'" . $ value . "'" ; } else { $ quotedValues [ ] = 'NULL' ; } } $ dataStringRows [ ] = "(" . implode ( "," , $ quotedValues ) . ")" ; } $ columns = ArrayLib :: wrapElements ( $ columns , '`' ) ; $ query = "INTO `" . $ tableName . "` (" . implode ( ',' , $ columns ) . ") " . "VALUES " . implode ( "," , $ dataStringRows ) ; return $ query ; }
Helper function to generateBatchReplaceQuery and generateBatchInsertQuery which are 99%
exactly the same except for the word REPLACE or INSERT.
@param array $rows - the data we wish to insert/replace into the database.
@param string tableName - the name of the table being manipulated.
@param \mysqli $mysqli - the database connection that would be used for the query.
@return string - the generated query.
@throws \Exception - if there is no data in the rows table and thus no query to generate.
Helper function to generateBatchReplaceQuery and generateBatchInsertQuery which are 99% exactly the same except for the word REPLACE or INSERT .