src/Mysql.php
protected function createEntry()
    {
        $arrColumns = $this->getTableColumns();
        $arrColumnNames = [];
        $arrColumnTypes = [];
        
        foreach ($arrColumns as $column) {
            $arrColumnNames[] = $column['Field'];
            $arrColumnTypes[] = $column['Type'];
        }
        
        $sql = 'INSERT INTO `'.$this->tableName.'` '
            . '(`'.implode('`, `', $arrColumnNames).'`) '
            . 'VALUES '
            . '(:'.implode(', :', $arrColumnNames).')';
        
        $arrInsert = [];
        for ($i = 0; $i < count($arrColumnNames); $i++) {
            $name = $arrColumnNames[$i];
            $value = isset($this->$name)?$this->$name:"";
            if (substr($arrColumnTypes[$i], 0, 3) == 'int') {
                $value = (int) $value;
            } elseif ($arrColumnTypes[$i] == 'datetime') {
                $value = strftime("%Y-%m-%d %H:%M:%S", strtotime($value));
            }
            $arrInsert[':'.$name] = $value;
        }
        
        
        $this->db->beginTransaction();
        $stmt = $this->db->prepare($sql);
        
        try {
            $stmt->execute($arrInsert);
            $this->db->commit();
        } catch (PDOException $ex) {
            $this->db->rollBack();
            $this->log->critical(
                "Could not save to mysql".PHP_EOL
                .$sql.PHP_EOL
                .print_r($arrInsert, true).PHP_EOL
                .$ex->getMessage()
            );
        }
    }
protected function createEntry()
    {
        $arrColumns = $this->getTableColumns();
        $arrColumnNames = [];
        $arrColumnTypes = [];
        
        foreach ($arrColumns as $column) {
            $arrColumnNames[] = $column['Field'];
            $arrColumnTypes[] = $column['Type'];
        }
        
        $sql = 'INSERT INTO `'.$this->tableName.'` '
            . '(`'.implode('`, `', $arrColumnNames).'`) '
            . 'VALUES '
            . '(:'.implode(', :', $arrColumnNames).')';
        
        $arrInsert = [];
        for ($i = 0; $i < count($arrColumnNames); $i++) {
            $name = $arrColumnNames[$i];
            $value = isset($this->$name)?$this->$name:"";
            if (substr($arrColumnTypes[$i], 0, 3) == 'int') {
                $value = (int) $value;
            } elseif ($arrColumnTypes[$i] == 'datetime') {
                $value = strftime("%Y-%m-%d %H:%M:%S", strtotime($value));
            }
            $arrInsert[':'.$name] = $value;
        }
        
        
        $this->db->beginTransaction();
        $stmt = $this->db->prepare($sql);
        
        try {
            $stmt->execute($arrInsert);
            $this->db->commit();
        } catch (PDOException $ex) {
            $this->db->rollBack();
            $this->log->critical(
                "Could not save to mysql".PHP_EOL
                .$sql.PHP_EOL
                .print_r($arrInsert, true).PHP_EOL
                .$ex->getMessage()
            );
        }
    }
protected function createEntry ( ) { $ arrColumns = $ this -> getTableColumns ( ) ; $ arrColumnNames = [ ] ; $ arrColumnTypes = [ ] ; foreach ( $ arrColumns as $ column ) { $ arrColumnNames [ ] = $ column [ 'Field' ] ; $ arrColumnTypes [ ] = $ column [ 'Type' ] ; } $ sql = 'INSERT INTO `' . $ this -> tableName . '` ' . '(`' . implode ( '`, `' , $ arrColumnNames ) . '`) ' . 'VALUES ' . '(:' . implode ( ', :' , $ arrColumnNames ) . ')' ; $ arrInsert = [ ] ; for ( $ i = 0 ; $ i < count ( $ arrColumnNames ) ; $ i ++ ) { $ name = $ arrColumnNames [ $ i ] ; $ value = isset ( $ this -> $ name ) ? $ this -> $ name : "" ; if ( substr ( $ arrColumnTypes [ $ i ] , 0 , 3 ) == 'int' ) { $ value = ( int ) $ value ; } elseif ( $ arrColumnTypes [ $ i ] == 'datetime' ) { $ value = strftime ( "%Y-%m-%d %H:%M:%S" , strtotime ( $ value ) ) ; } $ arrInsert [ ':' . $ name ] = $ value ; } $ this -> db -> beginTransaction ( ) ; $ stmt = $ this -> db -> prepare ( $ sql ) ; try { $ stmt -> execute ( $ arrInsert ) ; $ this -> db -> commit ( ) ; } catch ( PDOException $ ex ) { $ this -> db -> rollBack ( ) ; $ this -> log -> critical ( "Could not save to mysql" . PHP_EOL . $ sql . PHP_EOL . print_r ( $ arrInsert , true ) . PHP_EOL . $ ex -> getMessage ( ) ) ; } }
Inset a new Row into DB Table
Inset a new Row into DB Table