<code><span><span><?php</span><span>$dbhost</span> =<span>"localhost"</span>; <span>$dbuser</span> = <span>"root"</span>; <span>$dbpwd</span> = <span>"123"</span>; <span>$dbname</span> = <span>"mydatabase"</span>; <span>$charName</span> = <span>"utf8"</span>; <span>try</span> { <span>$conn</span> = <span>new</span> PDO(<span>"mysql:host=$dbhost;port=3306;dbname=$dbname"</span>, <span>$dbuser</span>, <span>$dbpwd</span>, <span>array</span>(PDO::MYSQL_ATTR_INIT_COMMAND => <span>"set names utf8"</span>)); <span>//echo "数据库链接成功!"; </span> } <span>catch</span>(PDOException <span>$e</span>){ <span>echo</span><span>$e</span>->getMessage(); } <span>?></span></span></span></code>
The above introduces how to use php pdo to connect to a MySQL database, including the Mysql database and the content of connecting to MySql. I hope it will be helpful to friends who are interested in PHP tutorials.