PHP Classes

File: examples/7-select_2.php

Recommend this page to a friend!
  Packages of MarPlo   PDO_MySQLi class   examples/7-select_2.php   Download  
File: examples/7-select_2.php
Role: Example script
Content type: text/plain
Description: Example 7
Class: PDO_MySQLi class
Access MySQL using PDO or MySQLi extensions
Author: By
Last change:
Date: 13 years ago
Size: 1,351 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);

/*
  for values used in LIKE statement, add the "?" or "%" characters in the value. Use like this
  $val = '%'. $val .'?';
*/

// SELECT with named placeholders and values for placeholders added into an associative array
$sql = "SELECT * FROM `testclass` WHERE `title` LIKE :title ORDER BY `id`";
$vals = array('title'=> '%Course%');

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

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

// if there are returned rows, traverses the array with rows data, using foreach()
if($nr_rows > 0) {
  echo 'Number of selected rows: '. $nr_rows .' / Number of columns: '. $nr_cols;

  // outputs data using index number of column order (index starting from 0)
  foreach($rows AS $row) {
    echo '<br/>Col1 = '. $row[0] .' / Col2 = '. $row[1] .' / Col3 = '. $row[2] .' / Col4 = '. $row[3];
  }
}
else {
  if($conn->error) echo $conn->error;      // if error, outputs it
  echo '0 selected rows';
}