dpdhsq0783 2015-07-23 18:25
浏览 52
已采纳

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

Can someone please help me understand why the below function works in option.php but does not work in main.php? From what I'm seeing, it looks as if the query is not going through when the function is called on main.php; however, it clearly goes through on 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条回答 默认 最新

  • douxin8610 2015-07-23 18:32
    关注

    The included nav.php file contained a mysqli_close call and terminated the connection before the fetch_options() function was run.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug