自定义PHP函数除了在一个特定页面上工作

时间:2015-07-23 18:25:20

标签: php mysqli

有人可以帮助我理解为什么以下功能在option.php中有效但在main.php中不起作用?从我所看到的情况来看,当在main.php上调用函数时,看起来好像没有查询;但是,它明确地通过了option.php。

custom_functions.php

function fetch_options(){
    global $connection;
    $query = "SELECT * FROM table";
    $result = mysqli_query($connection, $query);
    while($row = mysqli_fetch_assoc($result)){
        echo "<option value=\"{$row['a']}\">{$row['b']}</option>";
    }
    mysqli_free_result($result);
    mysqli_close($connection);
}

option.php

<?php 
require_once('lib/db.php');
require_once('lib/custom_functions.php');

echo "<select><option value=\"0\">Unknown</option>";
fetch_options();
echo "</select>";
?>

main.php

<?php 
require_once('lib/db.php');
require_once('lib/custom_functions.php');
?>  

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Main</title>
    <link href="css/bootstrap.css" rel="stylesheet">

    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>

    <?php include('lib/nav.php'); ?>

    <div class="container-fluid">   
    <div class="row">
    <div class="col-lg-2" style="padding:none;background-color:#ccc;min-height: calc(100vh - 52px);">
    </div>
    <div class="col-lg-10" style="padding:none;" id="page2">
    <select class="form-control" id="e_CR" name="craft">
        <option value="0">Unknown</option>
        <?php fetch_options(); ?>
    </select>
    </div>
    </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="js/validate.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/jfunctions.js"></script>

  </body>
</html>

1 个答案:

答案 0 :(得分:0)

包含的nav.php文件包含mysqli_close调用,并在运行fetch_options()函数之前终止连接。