PHP Classes

File: examples/3-insert_2rows.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Classes of MarPlo  >  PDO_MySQLi class  >  examples/3-insert_2rows.php  >  Download  
File: examples/3-insert_2rows.php
Role: Example script
Content type: text/plain
Description: Example 3
Class: PDO_MySQLi class
Access MySQL using PDO or MySQLi extensions
Author: By
Last change:
Date: 2 years ago
Size: 1,396 bytes


Class file image Download
// includes the file that contains data for connecting to mysql database, and PDO_MySQLi class

// creates object with connection to MySQL
$conn = new PDO_MySQLi($mysql);

// INSERT two rows in the same query
// with corresponding placeholder names in the SQL statement, and array with values for corresponding names
$sql = "INSERT INTO `testclass` (`url`, `title`, `dt`) VALUES (:url1, :title1, :dt1), (:url2, :title2, :dt2)";
$vals = array(
'url1'=>'', 'title1'=>'PHP-MySQL free course', 'dt1'=>time(),
'url2'=>'', 'title2'=>'JavaScript & jQuery Course', 'dt2'=>time()

// executes the SQL query, passing the SQL query and the array with values
$resql = $conn->sqlExecute($sql, $vals);

 when there are inserted multiple rows in the same query, the last insert id in an auto-increment column will be the id number of the first inserted row. In this case, to get the real last insert id, add to $last_insertid the number of inserted rows less one.
$last_id = $conn->last_insertid + (2 - 1);

// check if the SQL query succesfully performed
// displays the number of affected (inserted) rows, and the last auto_increment ID
if($resql) echo 'Inserted succesfully '. $conn->affected_rows .' rows. Last Auto-Increment ID: '. $last_id;
else echo