1

OK, so I have two files,

manageDay.php db_connect.php

db_connect.php is included on manageDay.php with require_once 'db_connect.php';

The files are in the same location (same folder)

The important parts of manageDay.php are as follows:

<?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'];
?>

This calls the included file which has database connect details, and a couple of functions.

<?

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

This second bit calls a record set which is returned as a result of a function in db_connect.php

Here is 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;
}
?>

The url called is manageDay.php?date=2015-01-02&user_id=2

When I call the page, it returns:

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

BUT, if I remove the function, and place the contents of it in manageDay.php it works fine?? Note that the database details are still being called from 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>
            ';
}
?>

Can anyone please help me understand why this is the case? I would not consider myself an accomplished coder by any means but if I'm missing something here...

Cheers

Joe
  • 111
  • 11
  • Your code is wide open to SQL injection. Please read [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/q/60174/3899908) and change from `mysql_` to `mysqli_` or `PDO`. – worldofjr Jan 08 '15 at 12:53

1 Answers1

0

Try

require_once('db_connect.php');

Also see mysqli for connecting: http://php.net/manual/en/function.mysqli-connect.php

  • 2
    this changes nothing, require_once with and without () both works. –  Jan 08 '15 at 12:43