I am calling a function in PHP that should return values from a database that I have set up. However, I'm getting a blank screen instead of what I'm asking for. If I comment the correct lines out and process it without the function, everything works perfectly. I've posted both examples below- hopefully somebody can notice what's wrong with my function.

Thanks!

This is the code that doesn't work (output is blank):

$month = "9";
$date = "23";
$num_teachers = 2;

$timeslot = "4:00";
$test = date_sched($timeslot);
print($test);


function date_sched($slot) {
    for($teacher_col = 1; $teacher_col <= $num_teachers; $teacher_col++) {
        $query_student_id = mysql_query("SELECT student_id FROM lesson_details WHERE teacher_id='$teacher_id[$teacher_col]' AND month='$month' AND date='$date' AND time='$slot'");
        if (mysql_num_rows($query_student_id) > 0) {
            $student_id = mysql_result($query_student_id,0);
            $query_student_name = mysql_query("SELECT name FROM students WHERE id='$student_id'");
            $student_name = mysql_result($query_student_name,0);
            $cell = $cell."<td><a href='main.php?page=man_other&manage=students&student_id=$student_id'>$student_name</a></td>";
        } else {
            $cell = $cell."<td>&nbsp;</td>";
        }
    }
    return $cell;
}

This is the code that does work:

$month = "9";
$date = "23";
$num_teachers = 2;
$slot = "4:00";

    for($teacher_col = 1; $teacher_col <= $num_teachers; $teacher_col++) {
        $query_student_id = mysql_query("SELECT student_id FROM lesson_details WHERE teacher_id='$teacher_id[$teacher_col]' AND month='$month' AND date='$date' AND time='$slot'");
        if (mysql_num_rows($query_student_id) > 0) {
            $student_id = mysql_result($query_student_id,0);
            $query_student_name = mysql_query("SELECT name FROM students WHERE id='$student_id'");
            $student_name = mysql_result($query_student_name,0);
            $cell = $cell."<td><a href='main.php?page=man_other&manage=students&student_id=$student_id'>$student_name</a></td>";
        } else {
            $cell = $cell."<td>&nbsp;</td>";
        }
    }

print($cell);

Output for code that does work (copied from page Source Code):
<td><a href='main.php?page=man_other&manage=students&student_id=4'>John Smith</a></td><td>&nbsp;</td>

Recommended Answers

All 7 Replies

the variables declared OUTSIDE the function do NOT exist within the function. So you need to pass all of them (INCLUDING the connection to the db - assuming somewhere you did [B]$dbConnection[/B]=mysql_connect(...) :

$test = date_sched($month,$date,$num_teachers,$timeslot,$dbConnection);

function date_sched($month,$date,$num_teachers,$slot,$dbConnection){
...
}

ALTERNATIVELY, you can use the global keyword to instruct the php engine to look for those variables OUTSIDE of the function:

function date_sched($slot){
  global $month,$date,$num_teachers,$dbConnection;
 ...
}

While developing it's also a good idea to turn display_errors on in php.ini.
Blank screen generally doesn't give you any feedback on what the errors are. Turning if off is generally a setting you want to use in your live/production environment.

Member Avatar for P0lT10n

the firts code dont work because you return $cell, you MUST return $cell but printed or echoed, dont do

return $cell;

do

echo $cell;

and you will recibe the $cell but visualy...

the firts code dont work because you return $cell, you MUST return $cell but printed or echoed, dont do

He is returning $cell and saving it onto $test, THEN he has print($test); . So in his case he needs to return $cell. His problem is related to variable scoping.

While developing it's also a good idea to turn display_errors on in php.ini.
Blank screen generally doesn't give you any feedback on what the errors are. Turning if off is generally a setting you want to use in your live/production environment.

I do have display_errors set as 'on' in php.ini, just for reference.

Thanks for the quick replies, everyone. I'll check my variable placement and update the thread then.

By the way, a quick question in terms of declaring my variables within the function as opposed to outside of it- a few variables used come from '$_GET[ ]' and a few more from includes. Does PHP allow the use of retrieving/declaring variables from 'include' and '$_GET[ ]' within a function?

Thanks again,
Ty

The variables YOU declare are not visible within the functions. But others like $_GET, $_POST, $_SERVER, etc, are "special". They are visible everywhere without the need to "declare them"/"use the global keyword" within functions.

As for your include, consider this:

file1.php
<?php
$greeting="Hello";
?>

file2.php
<?php
include("file1.php");

echo $greeting;//variable from file1.php visible here

sayItAgainIfYouCan();

function sayItAgainIfYouCan()
{
  echo $greeting;//but NOT visible here. You will need "global" keyword
}
?>

Thanks everyone (especially hielo)! I got it figured out.

hielo was right- as soon as the vars were placed inside the function, it ran perfectly.

Thanks again,
Ty

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.