You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# the first argument is the named placeholder name - notice named
# placeholders always start with a colon.
$STH->bindParam(':name', $name);
### Alternative 2:
# the data we want to insert
$data = array( 'name' => 'Cathy', 'addr' => '9 Dark and Twisty', 'city' => 'Cardiff' );
# the shortcut!
$STH = $DBH->("INSERT INTO folks (name, addr, city) value (:name, :addr, :city)");
$STH->execute($data);
### Alternative 3:
# ability to insert objects directly into your database, assuming the properties match the named fields.
# a simple object
class person {
public$name;
public$addr;
public$city;
function__construct($n,$a,$c) {
$this->name = $n;
$this->addr = $a;
$this->city = $c;
}
# etc ...
}
$cathy = newperson('Cathy','9 Dark and Twisty','Cardiff');
# here's the fun part:
$STH = $DBH->("INSERT INTO folks (name, addr, city) value (:name, :addr, :city)");
$STH->execute((array)$cathy);
}
functionselect_data_modes(){
/*
PDO::FETCH_ASSOC: returns an array indexed by column name
PDO::FETCH_BOTH (default): returns an array indexed by both column name and number
PDO::FETCH_BOUND: Assigns the values of your columns to the variables set with the ->bindColumn() method
PDO::FETCH_CLASS: Assigns the values of your columns to properties of the named class. It will create the properties if matching properties do not exist
PDO::FETCH_INTO: Updates an existing instance of the named class
PDO::FETCH_LAZY: Combines PDO::FETCH_BOTH/PDO::FETCH_OBJ, creating the object variable names as they are used
PDO::FETCH_NUM: returns an array indexed by column number
PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names
*/
/*
These three which will cover most situations: FETCH_ASSOC, FETCH_CLASS, and FETCH_OBJ.
*/
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
functionfetch_assoc(){
#This fetch type creates an associative array, indexed by column name.
# using the shortcut ->query() method here since there are no variable
# values in the select statement.
$STH = $DBH->query('SELECT name, addr, city from folks');
# setting the fetch mode
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($row = $STH->fetch()) {
echo$row['name'] . "\n";
echo$row['addr'] . "\n";
echo$row['city'] . "\n";
}
}
functionfetch_obj(){
#This fetch type creates an object of std class for each row of fetched data.
# creating the statement
$STH = $DBH->query('SELECT name, addr, city from folks');
# setting the fetch mode
$STH->setFetchMode(PDO::FETCH_OBJ);
# showing the results
while($row = $STH->fetch()) {
echo$row->name . "\n";
echo$row->addr . "\n";
echo$row->city . "\n";
}
}
functionfetch_class(){
#This fetch method allows you to fetch data directly into a class of your choosing.
#the properties of your object are set BEFORE the constructor is called.
#If properties matching the column names don't exist, those properties will be created (as public) for you.
#So if your data needs any transformation after it comes out of the database,
# it can be done automatically by your object as each object is created. (via __consruct() method )
class secret_person {
public$name;
public$addr;
public$city;
public$other_data;
function__construct($other = '') {
// will be called after object has valid data in its properties