Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created May 31, 2017 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpfiddle/ece945bc9688ff645aebd69710295ba3 to your computer and use it in GitHub Desktop.
Save phpfiddle/ece945bc9688ff645aebd69710295ba3 to your computer and use it in GitHub Desktop.
[ Posted by Don Stringham ] PDO initial introduction
<?php
/**
* PDO MySQL initial code
*
* User permissions of database
* Create, Alter and Index table, Create view, and Select, Insert, Update, Delete table data
*
* @package PhpFiddle
* @link http://phpfiddle.org
* @since 2012
*/
require_once "dBug!.php";
require "util/public_db_info.php";
$connect = new PDO($dsn, $user_name, $pass_word);
//get all tables in the database
$sql = "SHOW TABLES";
//get column information from a table in the database
//$sql="SELECT COLUMN_KEY, COLUMN_NAME, COLUMN_TYPE FROM information_schema.COLUMNS WHERE TABLE_NAME = 'books'";
//SQL statement for a table data
//$sql = "SELECT * FROM books WHERE id <= 10";
$result = $connect->prepare($sql);
//bind parameter(s) to variable(s)
//$result->bindParam( . . . );
$status = $result->execute();
//Next line is same with code from line 29 to 34 for one-time query
//$result = $connect->query($sql);
if (($status) && ($result->rowCount() > 0))
{
$results = array();
//convert query result into an associative array
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
$results[] = $row;
}
//dump all data from associative array converted from query result
new dBug($results);
}
$connect = null;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment