检索通用变量时出错[关闭]

i am having a problem, i am receiving an error which says undefined variable although it was already defined, here is the snippet:

names.php

session_start();
$uname=$_SESSION['login'];

$host="localhost";
$username="root";
$password="";
$db_name="sampledb";
$tbl_name="tblsched";
$cd = date("F d");
$cd1 = date("Y-n-d");
$ctr = 0;

// retrieving values from cookies that were created is located before this function
function mymainfunc()
{
dateto();
datefrom();
sunday();
monday();
tuesday();
thursday();
wednesday();
friday();
satday();
$arrayto = array();
$arrayfrom = array();
$arraysu=array();
$arraysa=array();
$arraym=array();
$arraytu=array();
$arrayw=array();
$arrayth=array();
$arrayf=array();

$arrayto = dateto();
$arrayfrom = datefrom();
$arraysu=sunday();
$arraysa=satday();
$arraym=monday();
$arraytu=tuesday();
$arrayw=wednesday();
$arrayth=thursday();
$arrayf=friday();

for($x=0;$x<=count($arrayto);$x++)
{
echo "<tr>";
echo "<td align=$tdali bgcolor=$bgcolor>"+ $arrayfrom[x] + "-" + $arrayto[x] + "</td>";
echo "<td align=$tdali>" + $arraysu[x] + "</td>";
echo "<td align=$tdali>" + $arraym[x] + "</td>";
echo "<td align=$tdali>" + $arraytu[x] + "</td>";
echo "<td align=$tdali>" + $arrayw[x] + "</td>";
echo "<td align=$tdali>" + $arrayth[x] + "</td>";
echo "<td align=$tdali>" + $arrayf[x] + "</td>";
echo "<td align=$tdali>" + $arraysa[x] + "</td>"; 
echo "</tr>";
}
}

//the other functions are found before this function
function dateto()
{
$sql="SELECT SchedTimeTo FROM $tbl_name WHERE teacherID=(SELECT teacherID FROM tblteacher WHERE teacherName=$uname) AND SchedDateFrom<=$cd1 AND SchedDateTo>=$cd1";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
$row = mysql_fetch_array($result);

$STF = array();
$STF[] = $row;
return $STF;
}

displayhere.php

//i inserted this snippet inside the

<?php
include "names.php";
mymainfunc();
?>

any suggestions?? the other functions didn't return any errors though

it says undefined variable tbl_name in the sql part of the function dateto()

When calling globally defined variables you will need to make them accessible to the function by means of the globalkeyword:

<?php
$my_global_var = 42;

function doSomethingImportant() {
    global $my_global_var;

    // now you can safely use that variable:
    if ($my_global_var == 42) {
        //...
    }
}
?>

alternatively (as explained by other answerers) you can use the $_GLOBALS['my_global_var']-array to acchieve the goal:

<?php
$_GLOBALS['my_global_var'] = 42;

function doSomethingImportant() {

    // now you can safely use that variable:
    if ($_GLOBALS['my_global_var'] == 42) {
        //...
    }
}
?>

You should explicitly declare variable as global to ensure it's scope:

//the other functions are found before this function
function dateto()
{
    global $tbl_name;
    global $uname;
    //Add all you global-scoped variables
    $sql="SELECT SchedTimeTo FROM $tbl_name WHERE teacherID=(SELECT teacherID FROM  tblteacher WHERE teacherName=$uname) AND SchedDateFrom<=$cd1 AND SchedDateTo>=$cd1";
    $result=mysql_query($sql);

    $count=mysql_num_rows($result);
    $row = mysql_fetch_array($result);

    $STF = array();
    $STF[] = $row;
    return $STF;
}

You're trying to access a global variable within function scope, check out the second example on this page.

You can move $tbl_name = .. to the function, or put global $tbl_name; in just before the sql statement, or you can use $_GLOBALS['tbl_name'] instead of $tbl_name.