包含 PHP 文件的问题


issues with including php file

好的,所以我有两个文件,

管理日.phpdb_connect.php

db_connect.php包含在管理日.php require_once 'db_connect.php';

文件位于同一位置(同一文件夹)

管理日.php的重要部分如下:

<?php
require_once 'db_connect.php';
ini_set ("display_errors", "1");
error_reporting(E_ALL); 
mysql_connect($db_hostname, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
$user_id = $_GET['user_id'];
$date = $_GET['date'];
?>

这将调用包含的文件,该文件具有数据库连接详细信息和几个函数。

<?
while ($row = mysql_fetch_assoc(getHoursForDay($date, $user_id))) {
/*while ($row = mysql_fetch_assoc($result)) {*/
            echo '
            <tr>
                <td>'.$row['code'].'
                </td> 
                <td>'.$row['project'].'
                </td>
                <td>'.$row['job'].'
                </td>
                <td>'.$row['start_time'].'
                </td>
                <td>'.$row['end_time'].'
                </td>
                <td>'.$row['location'].'
                </td>
                <td></td>
            </tr>
            ';
}
?>

第二个位调用记录集,该记录集作为函数的结果返回db_connect.php

这是db_connect.php

<?php
$db_hostname = "localhost";
$db_username = "xxxxx_ts";
$db_password = "xxxxxxx";
$db_name = "xxxxx_ts";
function getHoursForDay($date, $user_id){
    $sql = "SELECT *
    FROM bt_hours
    INNER JOIN bt_location ON bt_hours.location_id = bt_location.id
    INNER JOIN bt_projects ON bt_hours.project_id = bt_projects.id
    WHERE bt_hours.user_id='$user_id' AND bt_hours.work_date LIKE '$date'";
    $result = mysql_query($sql)
    or die(mysql_error());
    return $result;
}
?>

调用的网址是manageDay.php?date=2015-01-02&user_id=2

当我调用页面时,它返回:

Fatal error: Call to undefined function getHoursForDay() in /var/www/vhosts/xxxxx.com/ts.xxxxx.com/api/sql/manageDay.php on line 53 

但是,如果我删除该功能,并将其内容放在manageDay中.php它工作正常吗?请注意,仍从db_connect.php调用数据库详细信息

<?
$sql = "SELECT *
    FROM bt_hours
    INNER JOIN bt_location ON bt_hours.location_id = bt_location.id
    INNER JOIN bt_projects ON bt_hours.project_id = bt_projects.id
    WHERE bt_hours.user_id='$user_id' AND bt_hours.work_date LIKE '$date'";
    $result = mysql_query($sql)
    or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
/*while ($row = mysql_fetch_assoc($result)) {*/
            echo '
            <tr>
                <td>'.$row['code'].'
                </td> 
                <td>'.$row['project'].'
                </td>
                <td>'.$row['job'].'
                </td>
                <td>'.$row['start_time'].'
                </td>
                <td>'.$row['end_time'].'
                </td>
                <td>'.$row['location'].'
                </td>
                <td></td>
            </tr>
            ';
}
?>

谁能帮我理解为什么会这样?无论如何,我都不会认为自己是一个有成就的编码员,但如果我在这里错过了什么......

干杯

试试

require_once('db_connect.php');

另请参阅 mysqli 进行连接:http://php.net/manual/en/function.mysqli-connect.php