Connect to the MySQL database using PDO

Source: Internet
Author: User
Tags dsn error handling

<?php//Use PDO to connect to MySQL databaseClass pdo_con{var $dsn = ' mysql:dbname=test; host: '; 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-&GT;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.