2014-05-17 66 views
-3

如果我寫這樣的代碼:mysqli_connect()如何在PHP中工作?

mysqli_connect(host, username, password, dbname); 

的MySQL的這個功能應該連接到數據庫。但是,如果我把它改寫這個樣子,

$con = mysqli_connect(host, username, password, dbname); 

這個功能也做同樣的,以及它將其保存在一個變量,「$ CON」。

我的問題是:

$con = mysqli_connect(host, username, password, dbname); 

這段代碼不連接到服務器,只應保存在$ CON。當我們調用$ con時,該函數應該執行。當指定mysqli_connect()功能的結果您的變量$con,您創建和打開數據庫的連接

+0

除非那些(*預先定義*)常量,你需要包裝的加引號。否則,使用變量並首先定義它們。閱讀他們編寫的(* fabulous *)手冊http://www.php.net/manual/en/function.mysqli-connect.php,只是爲它;-) –

+0

我簡直不能理解這個網站。但感謝您的幫助! – aVIRA

+0

查找關於函數如何工作的文章。谷歌「具體返回值」。 – JJJ

回答

3
$con=mysqli_connect(host,username,password,dbname); 

上面的代碼不會實際產生的連接到數據庫。 但是,生成的連接確實需要檢查錯誤。 通常由以下幾點:

if(!$con) 
{ // creation of the connection object failed 
    die("connection object not created: ".mysqli_error($con)); 
} 

if (mysqli_connect_errno()) 
{ // creation of the connection object has some other error 
    die("Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error()); 
} 

然後,只是爲了讓事情變得有趣... 在代碼中的變量名是從該函數的原型字段名和NOT需要實際傳遞的值。

這裏是mysqli_connect()函數的一個更有用的執行

$myHost = "myDomainName.com"; // use your real host name 
$myUserName = "myUserName"; // use your real login user name 
$myPassword = "myPassword"; // use your real login password 
$myDataBaseName = "myDataBaseName"; // use your real database name 

$con = mysqli_connect("$myHost", "$myUserName", "$myPassword", "$myDataBaseName"); 

if(!$con) // == null if creation of connection object failed 
{ 
    // report the error to the user, then exit program 
    die("connection object not created: ".mysqli_error($con)); 
} 

if(mysqli_connect_errno()) // returns false if no error occurred 
{ 
    // report the error to the user, then exit program 
    die("Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error()); 
} 

// when got here, successfully connected to database 

Documentation

0

要回答你的問題。除非您在腳本中釋放此連接,否則該連接將保持打開狀態,直到腳本完成執行。

如果你想訪問的功能,當你需要打開一個連接你只能調用,你可能會認爲有關創建包裝函數:

function getDBConnection() 
{ 
    return mysqli_connect($host, $user, $pass, $db); 
} 
... 

$connection = getDBConnection(); 

你可能是包含此功能的文件到您的程序需要訪問數據庫的每個腳本中。請注意,您只需要爲每個文件調用一次getDBConnection()。然後,只要需要在該文件中訪問該連接,就可以重新使用該連接。

1

如果我理解正確,只想在實際需要時連接到數據庫。要做到這一點,你可以使用一個函數來包裝連接。如果你使這個功能足夠聰明,它只會建立連接一次,並在腳本持續時間內記住它。我已經逐行評論它來解釋發生了什麼。

<?php 
function con() 
{ 
    // Static variables are remembered between function calls. Otherwise, each call 
    // to the function would make a new connection. 
    static $con = null; 

    // Checks if there is a value assigned from a previous call. 
    if ($con === null) 
    { 
     // If no connection was made before, it is made now and assigned to 
     // the static variable $con. Make sure to fill in the right user, password and 
     // database here. 
     $con = mysqli_connect('localhost', 'DBUserName', 'passw', 'DatabaseToUse'); 
    } 
    // The connection is returned, whether it was made now or during a previous call. 
    return $con; 
} 

在代碼中,你可以使用這個功能,而不是$con變量,因此你可以通過它來mysqli_query例如:

mysqli_query(con(), 'SELECT * FROM ATable'); 
+0

不要$ con變量必須聲明並在函數外初始化? – Guybrush

+1

不是。它是靜態的。所有的細節都在答案中。 – GolezTrol