Home > Article > Backend Development > Use of PDO_PHP Tutorial
//You must first connect to the mysql database //Execute a query //Also: //Read the entire record set into the array: ) can also be used
if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == mysql) { print "Unquoted string: $string
"; //You see even the quotation marks are automatically added now...
}
$dbh = new PDO(mysql:host=localhost;dbname=test, $user, $pass);
//If you want to connect to mssql:
//mssql:host=localhost;dbname=testdb
//Connect pgsql:
//pgsql:host=localhost port=5432 dbname=testdb user=bruce password=mypass
//Connect odbc(DSN)
//odbc:testdb
//Connect access:
//odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:db.mdb;Uid= Admin
//There are also oracle, sqlite, db2....
foreach ($dbh->query(SELECT * from FOO) as $row) {
print_r($row); //This result is similar to mysql_fetch_array. PDOStatement::setFetchMode can be adjusted.
}
$sth = $dbh->prepare("SELECT name, color FROM fruit");
$sth->execute();
$result = $sth->fetchAll();
print_r($result);
//Output:
Array
(
[0] => Array
(
. [1] => green
)
[1] => Array
(
)
//Insert / delete / update data:
$count = $dbh->exec("DELETE FROM fruit WHERE color = red");
//$count is the number of deleted items. Equivalent to mysql_affected_rows
echo "Running on mysql; doing something mysql specific here
";
}
print "Quoted string: " . $conn->quote($string) . "
";
//Get:
Unquoted string: Nice
//Note that in different The results in the database are different, for example, some =>, some =>, =>gt;
//No worries now, it’s fully automatic.
//Finally I have to close it
$conn = null;
//But! You can stay connected:
$dbh = new PDO(odbc:SAMPLE, db2inst1, ibmdb2,
array(PDO_ATTR_PERSISTENT => true));
Attachment: A very simple special calling method:
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array ($_GET[name]))) { //What are you afraid of? Automatic quote!
while ($row = $stmt->fetch()) {
Also:
$stmt->bindParam(1, $id);
$stmt->bindParam(2, $_FILES[file][type]);
$stmt- >bindParam(3, $fp, PDO::PARAM_LOB);
Where can I find such a good function? php5.1 and above are in the extension, php5 is in pecl, php4? Don’t think about it, no.