1. <?php
    2. class Connect_mysql
    3. {
    4. /* 成员变量 */
    5. var $info;
    6. var $pdo;
    7. function connect()
    8. {
    9. try {
    10. $this->pdo = new PDO(
    11. "mysql:host=" . $this->info["host"] . ";dbname=" . $this->info["db_name"],
    12. $this->info["db_user"],
    13. $this->info["db_pwd"]
    14. ); //创建一个pdo对象
    15. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 设置sql语句查询如果出现问题 就会抛出异常
    16. //set_exception_handler("cus_exception_handler");
    17. } catch (PDOException $e) {
    18. die("connect error:" . $e->getMessage());
    19. }
    20. $this->pdo->exec("set names 'utf8'");
    21. }
    22. function init()
    23. {
    24. $this->info = array(
    25. 'host' => 'db',
    26. 'db_name' => 'frp',
    27. 'db_user' => 'root',
    28. 'db_pwd' => 'password',
    29. );
    30. }
    31. function __construct()
    32. {
    33. $this->init();
    34. $this->connect();
    35. }
    36. }