在 PHP 上连接 MySQL [英] MySQL connect on PHP

查看:22
本文介绍了在 PHP 上连接 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MySQL 上连接 PHP 应用程序的最佳方式是什么.

what is the best way to connect PHP application on MySQL.

到目前为止,我有以下连接类.

So far I had the below connection classes.

class Connection{
    private static $server = "127.0.0.1";
    private static $catalog = "schemadb";
    private static $username = "rootuser";
    private static $password = "password";
    public static $current = null;

    public static function Open(){
        self::$current = mysqli_init();
        if(!self::$current){
            die("Failed to initialize connection");
        }
        if(!self::$current->real_connect(self::$server,self::$username,self::$password,self::$catalog)){
            die("Cannot connect to server");
        }
        return self::$current;
    }
    public static function Close(){
        self::$current->close();
    }
}

我也有

abstract class abstractDAO
{
    protected function getConnection()
    {
        $mysqli = new mysqli("127.0.0.1","rootuser","password","schemadb");
        return $mysqli;
    }

}

或者是否有任何其他最佳方法可以在 MySQL 上连接 PHP 应用程序.请指教谢谢..

or if there's any other best approach to connect PHP application on MySQL. Please advise thanks..

推荐答案

您可以尝试使用 PDO 对象:

You can try using the PDO object:

<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

查看 PHP PDO 文档页面

这篇关于在 PHP 上连接 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆