Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created September 1, 2013 00:22
Show Gist options
  • Save phpfiddle/6401514 to your computer and use it in GitHub Desktop.
Save phpfiddle/6401514 to your computer and use it in GitHub Desktop.
Testing pdo
<?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
* @copyright Copyright (c) 2012 - 2013, PhpFiddle.org
* @link http://phpfiddle.org
* @since 2012
*/
require_once "dBug!.php";
$dsn = "mysql:host=localhost;dbname=xfiddlec_max;port=3306";
$user_name = "xfiddlec_user";
$pass_word = "public";
$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();
//for one-time query, this line is same with code from line 19 to 24
//$result = $connect->query($sql);
if (($status) && ($result->rowCount() > 0))
{
$results = array();
//convert query result into an associative array
while ($row = $result->fetchObject())
{
$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