-1

問題が発生しました。既に定義されているにもかかわらず、未定義の変数というエラーが表示されます。スニペットは次のとおりです。

名前.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と書かれています

4

3 に答える 3

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']-array を使用して目標を達成できます:

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

function doSomethingImportant() {

    // now you can safely use that variable:
    if ($_GLOBALS['my_global_var'] == 42) {
        //...
    }
}
?>
于 2012-09-18T06:50:02.643 に答える
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;
}
于 2012-09-18T06:49:13.133 に答える
0

関数スコープ内のグローバル変数にアクセスしようとしています。このページの 2 番目の例を確認してください。

$tbl_name = ..関数に移動するかglobal $tbl_name;、SQL ステートメントの直前に配置するか$_GLOBALS['tbl_name']、$tbl_name の代わりに使用することができます。

于 2012-09-18T06:49:27.070 に答える