<html>
<?php
/*** mysql hostname ***/
$hostname = 'localhost'; 

/*** mysql username ***/
$username = 'username';

/*** mysql ***/
$password = 'password';

$dbname = 'drinks';
if (isset($_GET['dbname'])) $dbname = $_GET['dbname'];
$tname = 'persons';
if (isset($_GET['tname'])) $fname = $_GET['tname'];
$fname = '%';
if (isset($_GET['fname'])) $fname = $_GET['fname'];

?>
<head>
<title>Three Different Approaches to MySql Queries - RJM Programming - June, 2014</title>
</head>
<body>
<h1 align='center'>Three Different Approaches to MySql Queries</h1>

<table border=10 cellpadding=10 cellspacing=10 style='background-color: pink;' align='center'>
<thead>
<tr><th>mysqli (objects)</th>
<?php 
 $ourhost="localhost"; //:8889";
 $ouruser="root";
 $ourpassword="root";
 $con = new mysqli($ourhost, $ouruser, $ourpassword,$dbname);
 // Check connection
 if (mysqli_connect_errno()) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

 $res = $con->query("SELECT * FROM " . $tname . " WHERE fname LIKE '$fname'");
 if ($res == 0) {
  echo("<b>Error " . mysqli_errno() . ": " . mysqli_error() . "</b>");
 } else if ($con->field_count == 0) {
  echo("<b>Query executed successfully</b>");
 } else {
    /* Get field information for all columns */
    $finfo = $res->fetch_fields();

    foreach ($finfo as $val) {
        echo("<th>" . $val->name . "</th>");
    }
    echo("</thead><tbody>");
    $res = $con->real_query("SELECT * FROM persons WHERE fname LIKE '$fname'");
    if ($result = $con->store_result()) {
        while ($row = $result->fetch_row()) {
          echo("<tr><td></td>");
          for ($jj = 0; $jj < $con->field_count; $jj++) {
            echo("<td>" . $row[$jj] . "</td>");
          }
          echo("</tr>");
        }
        $result->close();
    }
  
    echo("</tbody></table>");
 }
 $con->close($con);
?>

<br><br>
<table border=10 cellpadding=10 cellspacing=10 style='background-color: yellow;' align='center'>
<thead>
<tr><th>mysql (functional)</th>
<?php 
 $link = mysql_connect($hostname, $username, $password);
 if (!$link) {
    die('Could not connect: ' . mysql_error());
 }
 mysql_select_db($dbname);
 $res = mysql_query("SELECT * FROM " . $tname . " WHERE fname LIKE '$fname'");
 if ($res == 0) {
  echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
 } else if (mysql_num_rows($res) == 0) {
  echo("<b>Query executed successfully</b>");
 } else {
  for ($ii = 0; $ii < mysql_num_fields($res); $ii++) {
   echo("<th>" . mysql_field_name($res,$ii) . "</th>");
  } 
  echo("</thead><tbody>");
  while (($r_array = mysql_fetch_row($res))) {
   echo("<tr><td></td>");
   for ($jj = 0; $jj < mysql_num_fields($res); $jj++) {
    echo("<td>" . $r_array[$jj] . "</td>");
   }
   echo("</tr>");
  }
  echo("</tbody></table>");
 }
 mysql_close($link);
?>

<br><br>
<table border=10 cellpadding=10 cellspacing=10 style='background-color: lightgreen;' align='center'>
<thead>
<tr><th>PDO (objects)</th>
<?php 
try {
  $dbh = new PDO("mysql:host=$hostname;dbname=".$dbname, $username, $password);
  /*** echo a message saying we have connected ***/
  //echo 'Connected to database' . "\n";
  /*** The SQL SELECT statement ***/
  $fname = $_GET['fname'];
  
  $sql = 'select column_name from information_schema.columns where lower(table_name)=lower(\''.$tname.'\')';
  #$sql = 'SHOW COLUMNS FROM ' . $this->table;
  $stmt = $dbh->prepare($sql);

  try {    
      if($stmt->execute()){
         $raw_column_data = $stmt->fetchAll(PDO::FETCH_ASSOC);

         foreach($raw_column_data as $outer_key => $array){
            foreach($array as $inner_key => $value){
                if (!(int)$inner_key){
                    //$this->column_names[] = $value;
                    echo("<th>" . $value . "</th>");
                }
            }
        }
     }
   } catch (Exception $e){
        return $e->getMessage(); //return exception
   }        
  
   echo("</thead><tbody>");
  
  
   $STH = $dbh -> prepare( "SELECT * FROM " . $tname . " WHERE fname LIKE '$fname'" );
   if ($STH -> execute()) {
  
         $raw_column_data = $STH->fetchAll(PDO::FETCH_ASSOC);

         foreach($raw_column_data as $outer_key => $array){
            echo("<tr><td></td>");
            foreach($array as $inner_key => $value){
                if (!(int)$inner_key){
                    echo("<td>" . $value . "</td>");
                }
            }
            echo("</tr>");
        }
  
   }
   $dbh->close();
} catch(PDOException $e) {
   echo $e->getMessage();
 }
 echo("</tr></tbody></table>");
?>
</p>
</body>
</html> 
