mysqli_connect() 如何在 PHP 中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23716187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does mysqli_connect() work in PHP?
提问by aVIRA
If I write this code:
如果我写这个代码:
mysqli_connect(host, username, password, dbname);
This function of MySQL should connect to a database. But if I rewrite it like this,
MySQL 的这个函数应该连接到一个数据库。但如果我像这样重写它,
$con = mysqli_connect(host, username, password, dbname);
this function also do the same as well as it saves it in a variable, '$con'.
这个函数也做同样的事情,并将它保存在一个变量“$con”中。
My question is:
我的问题是:
$con = mysqli_connect(host, username, password, dbname);
This piece of code does not connect to the server and only should be saved in $con. When we call $con, then the function should execute.
这段代码没有连接到服务器,只应该保存在 $con 中。当我们调用 $con 时,该函数应该执行。
回答by user3629249
$con=mysqli_connect(host,username,password,dbname);
The above code does actually produce a connection to the database. However, the resulting connection does need to be checked for errors. Typically by the following:
上面的代码确实产生了到数据库的连接。但是,需要检查生成的连接是否有错误。通常通过以下方式:
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());
}
Then, just to make things interesting... the variable names in your code are from the prototype field names for the function and NOT the values that need to be actually passed.
然后,只是为了让事情变得有趣……代码中的变量名称来自函数的原型字段名称,而不是实际需要传递的值。
Here is a more useful execution of the mysqli_connect() function
这是 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
回答by GolezTrol
If I understand correctly, you want to only connect to the database when you actually need to. To do that, you can use a function to wrap the connection into. If you make this function smart enough, it will make the connection only once and remember it for the duration of the script. I've commented it line by line to explain what's going on.
如果我理解正确,您只想在实际需要时连接到数据库。为此,您可以使用一个函数来包装连接。如果你让这个函数足够聪明,它只会建立一次连接并在脚本的持续时间内记住它。我已经逐行评论它以解释发生了什么。
<?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;
}
In your code, you can use this function instead of the $con
variable, so you can pass it to mysqli_query for instance:
在您的代码中,您可以使用此函数而不是$con
变量,因此您可以将其传递给 mysqli_query 例如:
mysqli_query(con(), 'SELECT * FROM ATable');
回答by myesain
To answer your question:
回答你的问题:
When you assign the result of the mysqli_connect()
function to your variable $con
, you are creating and opening the connection to the database. Unless you release this connection within your script, that connection will remain open until the script finishes execution.
当您将mysqli_connect()
函数的结果分配给您的变量时$con
,您正在创建并打开与数据库的连接。除非您在脚本中释放此连接,否则该连接将保持打开状态,直到脚本完成执行。
If you wanted to have access to a function that you could invoke only when you need to open a connection, you might think about creating a wrapper function:
如果您想访问一个只能在需要打开连接时调用的函数,您可能会考虑创建一个包装函数:
function getDBConnection()
{
return mysqli_connect($host, $user, $pass, $db);
}
...
$connection = getDBConnection();
You could include a file containing this function into each script where your program will need to have access to your database. Just be aware that you'll only want to invoke the call to getDBConnection()
once per file. Then you can re-use that connection whenever you need access to it in that file.
您可以将包含此函数的文件包含到您的程序需要访问数据库的每个脚本中。请注意,您只想对getDBConnection()
每个文件调用一次。然后,只要您需要在该文件中访问该连接,就可以重新使用该连接。