Home  >  Article  >  Backend Development  >  Use of PDO_PHP Tutorial

Use of PDO_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:261079browse

//You must first connect to the mysql database
$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....

//Execute a query
foreach ($dbh->query(SELECT * from FOO) as $row) {
print_r($row); //This result is similar to mysql_fetch_array. PDOStatement::setFetchMode can be adjusted.
}

//Also:
$sth = $dbh->prepare("SELECT name, color FROM fruit");
$sth->execute();

//Read the entire record set into the array:
$result = $sth->fetchAll();
print_r($result);
//Output:
Array
(
[0] => Array
       (
                                                                                                                                           . [1] => green
)

[1] => Array
 ​ (

 ​ ​​​​​​​​​​​​​​​​​​​​​​=> pink

)

)

//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

// PDOStatement::rowCount

can also be used

//I forgot what database I used. . . .

if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == mysql) {
echo "Running on mysql; doing something mysql specific here ";
}

//Mysql_escape_string used to be used when inserting data, now?

print "Unquoted string: $string ";
print "Quoted string: " . $conn->quote($string) . " ";
//Get:
Unquoted string: Nice

Quoted string: Nice

//You see even the quotation marks are automatically added now...
//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));

//It’s very simple, isn’t it?


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()) {

print_r($row);

}

}


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.




http://www.bkjia.com/PHPjc/508227.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/508227.htmlTechArticle//First, connect to the mysql database $dbh = new PDO(mysql:host=localhost;dbname=test, $ user, $pass); //If you want to connect to mssql: //mssql:host=localhost;dbname=testdb //Connect to pgsql: //pgsql:hos...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn