<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>
以上就介紹了php pdo 連接MySQL資料庫,包括了Mysql資料庫,連接MySql方面的內容,希望對PHP教學有興趣的朋友有幫助。