dsij89625 2012-09-18 06:38
浏览 34
已采纳

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

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()

  • 写回答

3条回答 默认 最新

  • doog1092 2012-09-18 06:50
    关注

    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) {
            //...
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器