dpdhsq0783 2015-07-23 10: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 10:32
    关注

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

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部