包含php文件的问题

时间:2015-01-08 12:39:33

标签: php function

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

manageDay.php db_connect.php

db_connect.php包含在带有require_once 'db_connect.php';

的manageDay.php中

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

manageDay.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>
            ';
}
?>

任何人都可以帮助我理解为什么会这样吗?我不会认为自己是一个有成就的编码器,但如果我在这里遗漏了一些东西......

干杯

1 个答案:

答案 0 :(得分:0)

尝试

require_once('db_connect.php');

另见mysqli连接:http://php.net/manual/en/function.mysqli-connect.php