php 使用 mysqli 包含连接的正确方法

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

The correct way to include connection using mysqli

phpmysqli

提问by jaypabs

Below is my code under db.php

下面是我在 db.php 下的代码

    <?php
$con = mysqli_connect('localhost', 'root', '', 'mydb');

/* check connection */
if (!$con) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

?>

Under index.php I include db.php and functions.php as:

在 index.php 下,我将 db.php 和 functions.php 包括为:

include("includes/db.php");
include("includes/functions.php");

functions.php is also using the db.php connection. I don't have problem before when using mysql. But after I change mysql to mysqli I got an error "Warning: mysqli_query() expects parameter 1 to be mysqli, null given in" in my functions.php.

functions.php 也使用 db.php 连接。以前用mysql没有问题。但是在我将 mysql 更改为 mysqli 后,我在我的functions.php 中收到一个错误“警告:mysqli_query() 期望参数 1 为 mysqli,给定为空”。

This is the function under functions.php that has an error:

这是functions.php下有错误的函数:

function get_type($r_id){
    $result=mysqli_query($con, "select type from rooms where id=$r_id") or die("select type from rooms where id=$r_id"."<br/><br/>".mysqli_error());
    $row=mysqli_fetch_assoc($result);
    return $row['type'];
}

My solution is to add db.php in every function under functions.php that calls mysqli like:

我的解决方案是在functions.php下的每个调用mysqli的函数中添加db.php,例如:

function get_type($r_id){
    include("includes/db.php");
    $result=mysqli_query($con, "select type from rooms where id=$r_id") or die("select type from rooms where id=$r_id"."<br/><br/>".mysqli_error());
    $row=mysqli_fetch_assoc($result);
    return $row['type'];
}

I am wondering if this is the correct solution.

我想知道这是否是正确的解决方案。

回答by Dale

The problem is that $con is not available to your functions

问题是 $con 对您的功能不可用

You can add another argument to each function

您可以为每个函数添加另一个参数

function get_type($con, $r_id)...

and then pass $con into it

然后将 $con 传递给它

include('includes/db.php');
include('includes/functions.php');
$blah = get_type($con, 5);

OR

或者

You can make $con accessible to each function by adding this global $con;for example

例如,您可以通过添加它global $con;来使每个函数都可以访问 $con

function get_type($r_id){
    global $con;
    $result=mysqli_query($con, "select type from rooms where id=$r_id") or die("select type from rooms where id=$r_id"."<br/><br/>".mysqli_error());
    $row=mysqli_fetch_assoc($result);
    return $row['type'];
}

The choice my friend.. is yours

我的朋友的选择..是你的

(there may be other ways to skin this cat)

(可能还有其他方法可以给这只猫剥皮)

回答by Lucifer

set $conglobal in function:

$con在函数中设置全局:

function get_type($r_id){
    global $con;
    $result=mysqli_query($con, "select type from rooms where id=$r_id") or die("select type from rooms where id=$r_id"."<br/><br/>".mysqli_error());
    $row=mysqli_fetch_assoc($result);
    return $row['type'];
}