php 如何使mysqli连接功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15226019/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:50:04  来源:igfitidea点击:

How to make mysqli connect function?

phpmysqlmysqlidatabase-connection

提问by Jake Snake

I'm having a problem with a function that connects me to the database using mysqli. What I'm aiming to have is to just type getConnected();where I need the connection.

我有一个使用 mysqli 将我连接到数据库的函数的问题。我的目标是getConnected();在需要连接的地方输入。

This is the code:

这是代码:

function getConnected()
{
    $host = 'localhost';
    $user = 'logintest';
    $pass = 'logintest';
    $db = 'vibo';

    $mysqli = new mysqli($host, $user, $pass, $db);

    if ($mysqli->connect_error) {
        die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
    }
}

This is the error I get when I try to use $mysqliafter calling getConnected():

这是我$mysqli在调用后尝试使用时遇到的错误getConnected()

Notice: Undefined variable: mysqli in C:\xampp\htdocs\xampp\loginsystem\index.php on line 19

注意:未定义变量:mysqli in C:\xampp\htdocs\xampp\loginsystem\index.php 第 19 行

回答by Sam

As some users have suggested (and is the best way), return the mysqli instance

正如一些用户所建议的(也是最好的方法),返回 mysqli 实例

function getConnected($host,$user,$pass,$db) {

   $mysqli = new mysqli($host, $user, $pass, $db);

   if($mysqli->connect_error) 
     die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());

   return $mysqli;
}

Example:

例子:

$mysqli = getConnected('localhost','user','password','database');

回答by Dharman

You don't need to create such function. The connection to mysqli is only 3 lines of code when done properly. Definition and execution of such function would add twice as much.

您不需要创建这样的函数。如果正确完成,与 mysqli 的连接只需 3 行代码。此类功能的定义和执行将增加两倍。

Remember to always follow the three steps when opening a connection:

请记住在打开连接时始终遵循三个步骤:

  1. Enable mysqli error reporting.
  2. Create instance of mysqli class.
  3. Set the proper charset. Recommended one is utf8mb4
  1. 启用 mysqli 错误报告。
  2. 创建 mysqli 类的实例。
  3. 设置正确的字符集。推荐一个是utf8mb4

If you would like to have these 3 lines wrapped in a function you could do something like this:

如果您想将这 3 行包装在一个函数中,您可以执行以下操作:

function getConnected(): \mysqli {
    $host = 'localhost';
    $user = 'logintest';
    $pass = 'logintest';
    $db = 'vibo';

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $mysqli = new mysqli($host, $user, $pass, $db);
    $mysqli->set_charset('utf8mb4');

    // return the instance of mysqli class
    return $mysqli;
}

However, hardcoding the values does not make much sense. The values should be coming either from environment variables (e.g. read using getenv()function) or from config file. For example:

但是,对值进行硬编码并没有多大意义。这些值应该来自环境变量(例如使用getenv()函数读取)或来自配置文件。例如:

function getConnected(): \mysqli {
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $mysqli = new mysqli(getenv('DB_HOST'), getenv('DB_USER'), getenv('DB_PASS'), getenv('DB_NAME'));
    $mysqli->set_charset('utf8mb4');

    // return the instance of mysqli class
    return $mysqli;
}