<?php//Use PDO to connect to MySQL databaseClass pdo_con{var $dsn = ' mysql:dbname=test; host:127.0.0.1 '; var $user = ' root '; var $password = '; var $opt = array (pdo::attr_persistent=>true); var $dbh; function __construct () {try{$this->dbh = new PDO ($this->dsn, $this->user, $this->password, $th IS->OPT); Object-oriented construction method pass value this//var_dump ($DBH);d ie ();
}catch (pdoexception $e) {echo ' Database connection failed: '. $e->getmessage (); Exit } }
$rs = $dbh->query (' select * from user '),//Var_dump ($pdostatement->rowcount ());d ie (); Var_dump ($RS);
echo "\npdo Turn off autocommit feature:". $DBH->getattribute (Pdo::attr_autocommit); echo "\ n current PDO's error handling mode:". $DBH->getattribute (Pdo::attr_errmode); echo "\ n case conversion of table field characters:". $dbh->getattribute (pdo::attr_case); echo "\ n specific information related to connection status:". $DBH->getattribute (pdo::attr_connection_status); echo "\ n empty string converted to SQL null:". $DBH->getattribute (pdo::attr_oracle_nulls); echo "\ n the application gets the data size in advance:". $DBH->getattribute (pdo::attr_persistent); echo "\ n database-specific server information:". $DBH->getattribute (Pdo::attr_server_info); echo "\ nthe database server version information:". $DBH->getattribute (pdo::attr_server_version); echo "\ n Database client version number information:". $DBH->getattribute (pdo::attr_client_version); echo "\ n setting throws exception handling error:". $DBH->setattribute (pdo::attr_errmode,pdo::errmode_exception); echo "\ n Sets the empty string to null:". $DBH->setattribute (pdo::attr_oracle_nulls,true);
function Select ($table, $where = ', $order = ', $limits = ', $field = ') {$where = ($where ==null)? ': ' Where '. $where; $order = ($order ==null)? ': ' Order by '. $order; $limits = ($limits ==null)? ': ' Limit '. $limits; $field = ($field ==null)? ' * ': $field; $sele = ' SELECT '. $field. ' From '. $table. $where. $order. $limits. '; '; $kk = $this->dbh->query ($sele); $kk->setfetchmode (PDO::FETCH_ASSOC); Set fetch to return $KK after the query statement; }
}
$ss =new Pdo_con ();//Var_dump ($SS); $rs = $ss->select (' user '); foreach ($rs as $value) {# code ... var_dump ($value); echo $value [' username ']; Echo ' <br> ';}
From for notes (Wiz)
Connect to the MySQL database using PDO