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

Php 使用mysqli包含连接的正确方法,php,mysqli,Php,Mysqli,下面是我在db.php下的代码 <?php $con = mysqli_connect('localhost', 'root', '', 'mydb'); /* check connection */ if (!$con) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } ?> functions.php还使用db.php连接。我以前使用mysql时没有问题。但在我将m

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

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

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

?>
functions.php还使用db.php连接。我以前使用mysql时没有问题。但在我将mysql更改为mysqli后,在myfunctions.php中出现了一个错误“警告:mysqli_query()期望参数1为mysqli,null在中给定”

这是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'];
}
函数获取类型($r\u id){
$result=mysqli_query($con,“从id=$r_id的房间中选择类型”)或die(“从id=$r_id的房间中选择类型”。

。mysqli_error(); $row=mysqli\u fetch\u assoc($result); 返回$row['type']; }
我的解决方案是在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'];
}
函数获取类型($r\u id){
包括(“includes/db.php”);
$result=mysqli_query($con,“从id=$r_id的房间中选择类型”)或die(“从id=$r_id的房间中选择类型”。

。mysqli_error(); $row=mysqli\u fetch\u assoc($result); 返回$row['type']; }

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

问题是$con不可用于您的函数

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

function get_type($con, $r_id)...
然后将$con传递给它

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

通过添加此
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'];
}
函数获取类型($r\u id){
全球$con;
$result=mysqli_query($con,“从id=$r_id的房间中选择类型”)或die(“从id=$r_id的房间中选择类型”。

。mysqli_error(); $row=mysqli\u fetch\u assoc($result); 返回$row['type']; }
选择我的朋友。。是你的吗


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

设置
$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'];
}
函数获取类型($r\u id){
全球$con;
$result=mysqli_query($con,“从id=$r_id的房间中选择类型”)或die(“从id=$r_id的房间中选择类型”。

。mysqli_error(); $row=mysqli\u fetch\u assoc($result); 返回$row['type']; }
或die
类型的错误处理非常糟糕。使用单个包装器函数调用
mysqli\u query
,然后检查错误,在这种情况下记录/显示错误并存在!效果相当好;没有吗?这是一个相当不错的MySQL连接包装…还请注意,
MySQL\uu
mysqli\u
是不一样的,你不能只是“添加一个
i
”然后期望它工作;功能级别内是否安全?因为我已经读到使用全局是不可取的,如果它是在模块级的话。请纠正我的理解。thanks@jaypabs-请参阅,
global$var
的问题在于它们很容易被意外覆盖。我会警告您不要使用这种方法,尽管如果您真的非常小心,并且没有一个庞大的代码库覆盖多个文件(即,复杂度和出错的机会有限),它可能会工作。尝试寻找单例方法来设置/获取一个
$con
。我只是为@jaypabs问题提供了一些解决方案,我个人会在我的函数中添加另一个参数,并将已经设置的$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'];
}