【问题标题】:The code in an included file doesn't execute when uploaded to server上传到服务器时,包含文件中的代码不执行
【发布时间】:2014-12-11 08:45:54
【问题描述】:

我在 profile.php 中有这段代码:

$table_to_paginate  = 'updates';
$posts_per_page = 5;
require_once ("pagination/start_pagination.php");

$query  = "SELECT * FROM updates ";
$query .= " ORDER BY id DESC ";
$query .= " LIMIT {$start}, {$posts_per_page}";

在包含的文件中,即“pagination/start_pagination.php”我有这些代码:

//max displayed per page
            $per_page = $posts_per_page;
            //get start variable
            if(isset($_GET['start'])) {
            $start = $_GET['start'];
            }
            //count records
            if(!isset($additional_info)){$additional_info = NULL;}
            $counting = "SELECT * FROM {$table_to_paginate}";
            $counting .= "{$additional_info}";
            $record_count = mysql_num_rows(mysql_query($counting));
            //count max pages
            $max_pages = $record_count / $per_page; //may come out as decimal
            if (!isset($start)) {
                if(isset($upsidedown)){
                    if($record_count - $per_page < 0 ){ $start = 0;} else {$start = $record_count - $per_page;}
                } else {
                 $start = 0;
            }
            }

问题是,include 可以找到该文件,但它不提供包含文件中的变量,因此我可以在 profile.php 中使用它。有趣的是,它适用于本地主机,但是当我将其上传到服务器时它不起作用。

附加信息:我需要包含文件中的 $start。

【问题讨论】:

  • 服务器和localhost相差多少?
  • 如果你在包含的文件中加入echo 语句,你看到输出了吗?
  • 是的。 echo 有效,但即使在 echo 语句中,也无法从包含的文件中找到任何变量。

标签: php include server


【解决方案1】:

如果您想跨文件边界使用该变量,则需要通过 $GLOBALS 数组访问该变量:

// in profile.php
$query .= " LIMIT {$GLOBALS['start']}, {$posts_per_page}";

// in start_pagination.php
$per_page = $GLOBALS['posts_per_page'];
// ... snip
$counting = "SELECT * FROM {$GLOBALS['table_to_paginate']}";

【讨论】:

  • 这在我的问题中不起作用。这对我的情况没有帮助。
  • 我可以向你保证确实如此。
  • 我已更改代码以准确解决您的问题。更改您的代码,它应该可以工作。
猜你喜欢
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 2016-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多