不明白db connrction

时间:2016-06-01 12:20:30

标签: php mysql

我下载了这段代码,但我不理解编辑。

我想知道什么是$host$username

如果我得到公共服务器,我该如何更改

<?php
class Database
{

    private $host = "localhost";
    private $db_name = "test";
    private $username = "root";
    private $password = "";
    public $conn;

    public function dbConnection()
    {

        $this->conn = null;    
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}
?>

1 个答案:

答案 0 :(得分:3)

这是一个具有一些属性和方法的PHP类。由于没有实例化任何代码,代码本身并没有做任何事情。我重写了一点代码并添加了一些注释。让我解释一下:

//executes the command in the  working dir 
//and prints the output to a log file
def runProcess(workingDir, command) {
    ProcessBuilder procBuilder = new ProcessBuilder(command);
    procBuilder.directory(workingDir);
    procBuilder.redirectOutput(new File("${workingDir}/groovyGit.log"))

    Process proc = procBuilder.start();

    def exitVal = proc.waitFor()
    println "Exit value: ${exitVal}"

    return proc
}

//configuring the working dir for each git repository
def repoA = "repo A working dir"
def repoB = "repo B working dir"
def repoC = "repo C working dir"

//configuring the wanted revision to checkout for each repository
def repoARevision = "repo a revision"
def repoBRevision = "repo b revision"

def commitMsg = "commit msg"

//checkout A
runProcess(new File(repoA), ["git", "checkout", repoARevision])

//checkout B
runProcess(new File(repoB), ["git", "checkout", repoBRevision])

//check status before commit
runProcess(new File(repoC), ["git", "status"])

//commit
runProcess(new File(repoC), ["git", "commit", "-a", "-m", commitMsg])

现在,<?php // Define a class. We will use this class later to create an object. The object will create a database connection. class Database { // Some attributes (variables for all objects created) public $conn; public $dsn; public function connect($host, $db_name, $username, $password) { // We are using PDO, so before we continue, have a brief look at this: // http://php.net/manual/en/pdo.construct.php // Create the DSN $this->dsn = 'mysql:dbname=' . $db_name . ';host=' . $host; // Try the following code, but catch any errors in the next code block try { $this->conn = new PDO($this->dsn, $username, $password); // Set connection attributes. Learn more here: http://php.net/manual/en/pdo.setattribute.php $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { // echo the error if the try code block failed echo "Connection error: " . $e->getMessage(); } // Return the connection. You can assign a variable to the output of this method (function). return $this->conn; } } // Now instantiate the object to use the above code $db = new Database(); $test = $db->connect('localhost', 'test', 'root', 'password'); 可用于从数据库中读取,插入,更新和删除数据。而且,代码是可重用的。

$test

PDO文件:

https://github.com/MvvmCross/MvvmCross-Samples/tree/master/TipCalc(如何实例化PDO对象) http://php.net/manual/en/pdo.construct.php(整个文档)