Php MYSQLi_查询无法使用Includes文件夹中的数据库连接

Php MYSQLi_查询无法使用Includes文件夹中的数据库连接,php,mysqli,Php,Mysqli,我在文档根目录之外的includes文件夹中设置了一个dbconn.php文件。当我在select语句中引用$mysqli时,我收到一个错误 Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch mysqli in /home/tgitcorp/public_html/Admin/admin_index.php on line 18 Warning: mysqli_fetch_array() expects pa

我在文档根目录之外的includes文件夹中设置了一个dbconn.php文件。当我在select语句中引用$mysqli时,我收到一个错误

Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch mysqli     
in /home/tgitcorp/public_html/Admin/admin_index.php on line 18
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given 
in /home/tgitcorp/public_html/Admin/admin_index.php on line 20 
我的dbconn.php如下所示:

<?php
    $mysqli = new mysqli('localhost','dbuser','pass','dbname');
    if ($mysqli->connect_error){
        die('Connect Error (' . $mysqli_connect_errno . ')'. $mysqli->connect_error);
    }
    $mysqli->close();   
?>

为什么在实例化连接后立即关闭它?这一定与此有关。听起来$mysqli->close属于if块


编辑:在任何情况下,关闭连接都是可选的,如下所述:

尝试var_dump$mysqli;在mysql_查询之后,var_dump$result;是否在要使用的脚本中包含dbconn文件。包含_once path/to/dir/dbconn.php;?dbconn.php中有什么?您发布的代码中没有任何内容可以生成您报告的错误。添加了dbconn和完整错误消息您有一个奇怪的混合OO和过程式MySQLi调用,尽管这不是失败的原因。您创建了一个MySQLi对象,但以过程样式使用它。尝试$result=mysqli->查询“从餐厅选择位置”;以及您的fetch调用:$row=$mysqli->fetch\u数组$resultNice catch。这应该是问题所在。不应该放在脚本文件结尾处的结束处。@Michael,这也是一个有效的位置。然而,据我所知,MySQL连接会在PHP脚本结束时自动关闭。@Joel谢谢!!我的错误消息已解决。像我这样的noob需要一双额外的眼睛:现在我只需要填充下拉列表:@Joel是的,这是真的,连接应该在脚本结束时关闭。手动关闭连接也不是一个坏习惯。
<?php 
include_once '/home/tgitcorp/includes/dbconn.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML     
1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Tricorp Job Listing Admin Panel</title>
    <link rel="stylesheet" href="../css/style.css" type="text/css"/>
</head>
<body>
    <h1>Job Listing Administration</h1>
    <h2>Step 1:  Please Select Your Restaurant</h2>
    <form id="frmSelStore" method="post">
    <?php 
    $result=mysqli_query($mysqli,'SELECT location from restaurant');
    echo '<select name="ddlStore">';
    while($row=mysqli_fetch_array($result))
    {
        echo '<option value="' . htmlspecialchars($row['location']) . '"></option>';
    }
    echo '</select>';
    ?>
    </form>
</body>
</html>
<?php 
$result=$mysqli->query($mysqli,'SELECT location from restaurant');
echo '<select name="ddlStore">';
while($row=$mysqli->query($result))
{
    echo '<option value="' . htmlspecialchars($row['location']) . '">';
    '</option>';
}
echo '</select>';
?>
Warning: mysqli::query() expects parameter 1 to be string, object given 
in /home/tgitcorp/public_html/Admin/admin_index.php on line 18
Warning: mysqli::query() [mysqli.query]: Empty query 
in /home/tgitcorp/public_html/Admin/admin_index.php on line 20