PHP Classes
elePHPant
Icontem

File: examples/9-insert_select.php

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

Contents

Class file image Download
<?php
// includes the file that contains data for connecting to mysql database, and PDO_MySQLi class
include('../conn_mysql.php');

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

// Insert, with question marks placeholders, and values added into an array (in the same order associated to question marks)
$sql = "INSERT INTO `testclass` (`url`, `title`, `dt`) VALUES (?, ?, ?)";
$vals = array('http://coursesweb.net/', 'Free Web Courses', time());

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

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

 
// simple SELECT (without placeholders) using the last-inserted-id
 
$sql = "SELECT `id`, `title`, `url` FROM `testclass` WHERE `id` NOT IN( $last_id )";

 
// executes the SQL query using the same connection object, and gets the selected rows
 
$rows = $conn->sqlExecute($sql);

 
$nr_rows = $conn->num_rows; // number of selected rows

  // if there are returned rows, traverses the array with rows data, using for() instruction
 
if($nr_rows > 0) {
    for(
$i=0; $i<$nr_rows; $i++) {
     
$row = $rows[$i];
      echo
'<br/>ID = '. $row['id'] .' / URL = '. $row['url'] .' / Title = '. $row['title'];
    }
  }
  else {
    if(
$conn->error) echo $conn->error; // if error, outputs it
   
echo '0 selected rows';
  }
}
else echo
$conn->error;