Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $dbh = new
- PDO('mysql:host=localhost;dbname=test', $user, $pass);
- ?>
- <?php
- try {
- $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
- foreach
- ($dbh
- -
- >query('SELECT * from FOO') as $row) {
- print_r($row);
- }
- $dbh = null;
- } catch (PDOException $e) {
- print "Error!: " . $e
- -
- >getMessage() . "<br/>";
- die();
- }
- ?>
- <?php
- $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
- // use the connection here
- $sth = $dbh
- -
- >query('SELECT * FROM foo');
- // and now we're done; close it
- $sth = null;
- $dbh = null;
- ?>
- <?php
- $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
- PDO::ATTR
- _PERSISTENT => true
- ));
- ?>
- <?php
- try {
- $dbh = new PDO('mysql:host=localhost;dbname=test', 'user', 'password',
- array(PDO::ATTR_PERSISTENT => true));
- echo "Connected
- \
- n";
- } catch (Exception $e) {
- die("Unable to connect: " . $e
- -
- >getMessage());
- }
- try {
- $dbh
- -
- >setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $dbh
- -
- >beginTransaction();
- $dbh
- -
- >exec("INSERT INTO dept VALUES ('50','TEST','WASHNGTON')");
- $dbh
- -
- >exec("INSERT INTO emp VALUES ('7944','JOHN','CLERK','7782','1983
- -
- 01
- -
- 01','1000.00',NULL,
- '50')");
- $dbh
- -
- >commit();
- } catch (Exception $e) {
- $dbh
- -
- >rollBack();
- echo "Failed: " . $e
- -
- >getMessage();
- }
- ?>
- <?php
- $stmt = $dbh
- -
- >prepare("INSERT INTO REGISTRY (name, value) VALUES (:name,
- :value)");
- $stmt
- -
- >bindParam(':name', $name);
- $stmt
- -
- >bindParam(':va
- lue', $value);
- // insert one row
- $name = 'one';
- $value = 1;
- $stmt
- -
- >execute();
- // insert another row with different values
- $name = 'two';
- $value = 2;
- $stmt
- -
- >execute();
- ?>
- <?php
- $stmt = $dbh
- -
- >prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
- $stmt
- -
- >bindParam(1, $name);
- $stmt
- -
- >bindParam(2, $value);
- //
- insert one row
- $name = 'one';
- $value = 1;
- $stmt
- -
- >execute();
- // insert another row with different values
- $name = 'two';
- $value = 2;
- $stmt
- -
- >execute();
- ?>
- <?php
- $stmt = $dbh
- -
- >prepare("SELECT * FROM REGISTRY where name = ?")
- ;
- if ($stmt
- -
- >execute(array($_GET['name']))) {
- while ($row = $stmt
- -
- >fetch()) {
- print_r($row);
- }
- }
- ?>
- <?php
- $stmt = $dbh
- -
- >prepare("CALL sp_returns_string(?)");
- $stmt
- -
- >bindParam(1, $return_value, PDO::PARAM_ST
- R, 4000);
- // call the stored procedure
- $stmt
- -
- >execute();
- print "procedure returned $return_value
- \
- n";
- ?>
- <?php
- $stmt = $dbh
- -
- >prepare("CALL sp_take
- s_string_returns_string(?)");
- $value = 'hello';
- $stmt
- -
- >bindParam(1, $value, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000);
- // call the stored procedure
- $stmt
- -
- >execute();
- print "procedure returned $value
- \
- n";
- ?>
- <?php
- $stmt = $dbh
- -
- >prepare("SELECT * FROM REGISTRY where name LIKE '%?%'");
- $stmt
- -
- >execute(array($_GET['name']));
- // placeholder must be used in the place of the whole value
- $stmt = $dbh
- -
- >prepare("SELECT * FROM REGISTRY where name LIKE ?");
- $s
- tmt
- -
- >execute(array("%$_GET[
- '
- name
- '
- ]%"));
- ?><?php
- $stmt = $dbh
- -
- >prepare("SELECT * FROM REGISTRY where name LIKE '%?%'");
- $stmt
- -
- >execute(array($_GET['name']));
- // placeholder must be used in the place of the whole value
- $stmt = $dbh
- -
- >prepare("SELECT * FROM REGISTRY where name LIKE ?");
- $s
- tmt
- -
- >execute(array("%$_GET[
- '
- name
- '
- ]%"));
- ?>
- <?php
- $dsn = 'mysql:dbname=testdb;host=127.0.0.1';
- $user = 'dbuser';
- $password = 'dbpass';
- try {
- $dbh = new PDO($dsn, $user, $password);
- $dbh
- -
- >setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $e) {
- echo 'Connection failed: ' . $e
- -
- >getMessage();
- }
- ?>
- <?php
- $dsn = 'mysql:dbname=test;host=127.0.0.1';
- $user = 'googl
- eguy';
- $password = 'googleguy';
- /*
- Using try/catch around the constructor is still valid even though we set the
- ERRMODE to WARNING since
- PDO::__construct will always throw a PDOException if the connection fails.
- */
- try {
- $dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE =>
- PDO::ERRMODE_WARNING));
- } catch (PDOException $e) {
- echo 'Connection failed: ' . $e
- -
- >getMessage();
- exit;
- }
- // This will cause PDO to throw an error of level E_WARNING instead of
- an
- exception (when the table doesn't exist)
- $dbh
- -
- >query("SELECT wrongcolumn FROM wrongtable");
- ?>
- <?php
- $db = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2');
- $stmt = $db
- -
- >prepare("select contenttype, imagedata from images where id=?");
- $stmt
- -
- >execute(array($_GET['id']));
- $stmt
- -
- >bindColumn(1, $type, PDO::PARAM_STR, 256);
- $stmt
- -
- >bindColumn(2, $lob, PDO
- ::PARAM_LOB);
- $stmt
- -
- >fetch(PDO::FETCH_BOUND);
- header("Content
- -
- Type: $type");
- fpassthru($lob);
- ?>
- <?php
- $db = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2');
- $stmt = $db
- -
- >prepare("insert into images (id, contenttype, imagedata) values (?,
- ?, ?)");
- $id = g
- et_new_id(); // some function to allocate a new ID
- // assume that we are running as part of a file upload form
- // You can find more information in the PHP documentation
- $fp = fopen($_FILES['file']['tmp_name'], 'rb');
- $stmt
- -
- >bindParam(1, $id);
- $stmt
- -
- >bin
- dParam(2, $_FILES['file']['type']);
- $stmt
- -
- >bindParam(3, $fp, PDO::PARAM_LOB);
- $db
- -
- >beginTransaction();
- $stmt
- -
- >execute();
- $db
- -
- >commit();
- ?>
- <?php
- $db = new PDO('oci:', 'scott', 'tiger');
- $stmt = $db
- -
- >prepare("insert into images (id, contenttype, imagedata) " .
- "VALUES (?, ?, EMPTY_BLOB()) RETURNING imagedata INTO ?");
- $id = get_new_id(); // some function to allocate a new ID
- // assume that we are running as part of a file upload form
- // You can find more information in the PHP documentation
- $fp = fopen($_FILES
- ['file']['tmp_name'], 'rb');
- $stmt
- -
- >bindParam(1, $id);
- $stmt
- -
- >bindParam(2, $_FILES['file']['type']);
- $stmt
- -
- >bindParam(3, $fp, PDO::PARAM_LOB);
- $db
- -
- >beginTransaction();
- $stmt
- -
- >execute();
- $db
- -
- >commit();
- ?>
- <?php
- /* Begin a transaction, turning off autocommit */
- $dbh
- -
- >beginTransaction();
- /* Change the database schema and data */
- $sth = $dbh
- -
- >exec("DROP TABLE fruit");
- $sth = $dbh
- -
- >exec("UPDATE dessert
- SET name = 'hamburger'");
- /* Recognize
- mistake and roll back changes */
- $dbh
- -
- >rollBack();
- /* Database connection is now back in autocommit mode */
- ?>
- <?php
- /* Begin a transaction, turning off
- autocommit */
- $dbh
- -
- >beginTransaction();
- /* Insert multiple records on an all
- -
- or
- -
- nothing basis */
- $sql = 'INSERT INTO fruit
- (name, colour, calories)
- VALUES (?, ?, ?)';
- $sth = $dbh
- -
- >prepare($sql);
- foreach ($fruits as $fruit) {
- $sth
- -
- >execute(array(
- $fruit
- -
- >name,
- $fruit
- -
- >colour,
- $fruit
- -
- >calories,
- ));
- }
- /* Commit the changes */
- $dbh
- -
- >commit();
- /* Database connection is now back in autocommit mode */
- ?>
- <?php
- /* Begin a transaction, turning off autocommit */
- $dbh
- -
- >beginTransaction();
- /* Change the database schema */
- $sth = $dbh
- -
- >exec("DROP TABLE fruit");
- /* Commit the changes */
- $dbh
- -
- >commit();
- /* Database connection is now back in autocommit mode */
- ?>
- <?php
- /* Connect to a MySQL database using driver invocation */
- $dsn = 'mysql:dbname=testdb;host=127.0.0.1';
- $user = 'dbuser';
- $password = 'dbpass';
- try {
- $dbh = new P
- DO($dsn, $user, $password);
- } catch (PDOException $e) {
- echo 'Connection failed: ' . $e
- -
- >getMessage();
- }
- ?>
- <?php
- /* Provoke an error
- --
- the BONES table does not exist */
- $dbh
- -
- >exec("INSERT INTO bones(skull) VALUES ('lucy')");
- echo "
- \
- nPDO::errorCode(): ", $dbh
- -
- >errorCode();
- ?>
- <?php
- /* Provoke an error
- --
- bogus SQL syntax */
- $stmt = $dbh
- -
- >prepare('bogus sql');
- if (!$stmt) {
- echo "
- \
- nPDO::errorInfo():
- \
- n";
- print_r($dbh
- -
- >errorInfo());
- }
- ?>
- <?php
- print_r(PDO::getAvailableDrivers());
- ?>
- <?php
- $conn = new PDO('odbc:sample', 'db2inst1', 'ibmdb2');
- $attributes = array(
- "AUTOCOMMIT", "ERRMODE", "CASE", "CLIENT_VERSION", "CONNECTION_STATUS",
- "ORACLE_NULLS", "PERSISTENT", "PREFETCH", "SERVER_INFO", "SERVER_VERSION",
- "TIMEOUT"
- );
- foreach ($attributes as $val) {
- echo "PDO::ATTR_$val: ";
- echo $conn
- -
- >getAtt
- ribute(constant("PDO::ATTR_$val")) . "
- \
- n";
- }
- ?>
- <?php
- $conn = new PDO('sqlite:/home/lynn/music.sql3');
- /* Simple string */
- $string = 'Nice';
- print "Unquoted string: $string
- \
- n";
- print "Quoted string: " . $conn
- -
- >quote($string) . "
- \
- n";
- ?>
- <?php
- $conn = new PDO('sqlite:/home/lynn/music.sql3');
- /* Dangerous string */
- $string = 'Naughty
- \
- ' string';
- print "Unquoted string: $string
- \
- n";
- print "Quoted string:" . $conn
- -
- >quote($string) . "
- \
- n";
- ?>
- <?php
- function getFruit($conn) {
- $sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
- foreach ($conn
- -
- >query($sql) as $row) {
- print $row['name'] . "
- \
- t";
- print $row['color'] . "
- \
- t";
- print $row['calories'] . "
- \
- n";
- }
- }
- ?>
- <?php
- /* Execute a prepared statement by passing an array of values */
- $sql = 'SELECT name, colour, calories
- FROM fruit
- WHERE calories < :calories AND colour = :colour';
- $sth = $dbh
- -
- >prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
- $sth
- -
- >execute(array(':calories' => 150, ':colour' => 'red'));
- $red = $sth
- -
- >fetchAll();
- $sth
- -
- >execute(array(':calories' => 175,
- ':colour' => 'yellow'));
- $yellow = $sth
- -
- >fetchAll();
- ?>
- <?php
- /* Execute a prepared statement by passing an array of values */
- $sth = $dbh
- -
- >prepare('SELECT name, colour, calories
- FROM fruit
- WHERE calories < ? AND colo
- ur = ?');
- $sth
- -
- >execute(array(150, 'red'));
- $red = $sth
- -
- >fetchAll();
- $sth
- -
- >execute(array(175, 'yellow'));
- $yellow = $sth
- -
- >fetchAll();
- ?>
- <?php
- /*
- CREATE TABLE Guests (
- id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- firstname VARCHAR(30) NOT NULL,
- lastname VARCHAR(30) NOT NULL,
- email VARCHAR(50),
- reg_date TIMESTAMP
- )
- */
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $
- dbname = "test";
- try {
- $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
- $password);
- // set the PDO error mode to exception
- $conn
- -
- >setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- // prepare sql and bind paramete
- rs
- $stmt = $conn
- -
- >prepare("INSERT INTO Guests (firstname, lastname, email)
- VALUES (:firstname, :lastname, :email)");
- $stmt
- -
- >bindParam(':firstname', $firstname);
- $stmt
- -
- >bindParam(':lastname', $lastname);
- $stmt
- -
- >bindParam(':email', $emai
- l);
- // insert a row
- $firstname = "John";
- $lastname = "Doe";
- $email = "john@example.com";
- $stmt
- -
- >execute();
- // insert another row
- $firstname = "Mary";
- $lastname = "Moe";
- $email = "mary@example.com";
- $stmt
- -
- >execute();
- // insert another row
- $firstname = "Julie";
- $lastname = "Dooley";
- $email = "julie@example.com";
- $stmt
- -
- >execute();
- echo "New records created successfully";
- }
- catch(PDOException $e)
- {
- echo "Error: "
- . $e
- -
- >getMessage();
- }
- $conn = null;
- ?>
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "test";
- try {
- $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
- $password);
- $conn
- -
- >setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $stmt = $conn
- -
- >prepare("SELECT id, firstname, lastname FROM Guests");
- $stmt
- -
- >execute();
- // set the resulting array to associative
- $result = $stmt
- -
- >setFetchMode(PDO::FETCH
- _ASSOC);
- while($row = $stmt
- -
- >fetch()) {
- echo $row['id'] . "
- \
- t";
- echo $row['firstname'] . "
- \
- t";
- echo $row['lastname'] . "
- \
- t";
- echo "<br />";
- }
- }
- catch(PDOException $e) {
- echo "Error: " . $e
- -
- >getMessage();
- }
- $conn = null;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement