检索通用变量时出错

时间:2012-09-18 06:38:57

标签: php mysql

我遇到了问题,我收到的错误显示未定义的变量虽然已经定义了,但这里是片段:

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

//我将此代码段插入

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

任何建议?其他函数虽然没有返回任何错误

它表示函数dateto()

的sql部分中的未定义变量tbl_name

3 个答案:

答案 0 :(得分:2)

调用全局定义的变量时,您需要通过global关键字使函数可以访问它们:

<?php
$my_global_var = 42;

function doSomethingImportant() {
    global $my_global_var;

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

或者(正如其他回答者所解释的那样)你可以使用$_GLOBALS['my_global_var'] - 数组来达到目标​​:

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

function doSomethingImportant() {

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

答案 1 :(得分:0)

您应该将变量显式声明为全局,以确保其范围:

//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;
}

答案 2 :(得分:0)

您正在尝试访问功能范围内的全局变量,请查看this page上的第二个示例。

您可以将$tbl_name = ..移动到该函数,或者将global $tbl_name;放在sql语句之前,或者您可以使用$_GLOBALS['tbl_name']而不是$ tbl_name。