簡體   English   中英

設定變量的未定義變量錯誤

[英]Undefined variable error on a set variable

我是PHP的新手。 這是圖庫代碼。 盡管我使畫廊正常工作,但還是出現了一個錯誤,顯示為"PHP Notice: Undefined variable: caption_array in /home/th/public_html/gallery.php on line 13" 我檢查了一下,發現設置了caption_array ,但無法弄清楚為什么此錯誤持續出現。 下面的代碼是使用caption_array地方。

// display previous and next links if more than one photo 
else if( $pcaption ) 
{
    mysql_query("UPDATE gallery_photos SET photo_caption =    
    REPLACE(photo_caption,'\\\','') ");
    $pcaption = str_replace("-", " ",$pcaption);
    $pcaption = str_replace("%27", "'",$pcaption);
    $pcaption = str_replace("\\", "",$pcaption);
    $result = mysql_query( "SELECT photo_caption, photo_description, photo_filename,photo_keywords FROM gallery_photos WHERE photo_caption='".addslashes($pcaption)."'" ); 
    list($photo_caption, $photo_description, $photo_filename, $photo_keywords) = mysql_fetch_array( $result ); 

    $nr = mysql_num_rows( $result ); 
    mysql_free_result( $result );     

    $p_caption = $photo_caption;
    $p_description = $photo_description;
    $p_keywords = $photo_keywords;

    //fill caption_array with sorted pids in current category 

    $result = mysql_query( "SELECT photo_caption FROM gallery_photos WHERE category_name='".addslashes($cname)."' " ); 

    $ct = mysql_num_rows( $result ); 

    while ($row = mysql_fetch_array($result)) {
        $row[0]= trim($row[0]);
        $row[0] = str_replace(" ","-",$row[0]);
        $row[0] = str_replace("'","%27",$row[0]);
        $caption_array[] = trim($row[0]); 
    }

    mysql_free_result( $result );

    if( empty($nr ) ) 
    { 
        $result_final = "\t<tr><td>***No Photo found*******</td></tr>\n"; 
    } 
    else 
    { 
        $category_name = $cname; 
        $cname = str_replace(" ", "-", $cname); 
        $result_final = "
            <div class=limagePage>
                <div class=llink>
                    <a href=/gallery.php>ALBUMS</a>
                    <span class=arrow>&gt;&gt</span>
                    <a href=/gallery.php?cname=$cname>$category_name</a>
                </div>
        ";

        // display previous and next links if more than one photo 

        if ($ct > 1) 
        { 
            $pcaption = trim($pcaption);
            $pcaption = str_replace(" ","-",$pcaption);
            $pcaption = str_replace("'","%27",$pcaption);
            $key = array_search($pcaption , $caption_array); 
            $prev = $key - 1; 
            if ($prev < 0) $prev = $ct - 1; 
            $next = $key + 1; 

            if ($next == $ct) $next = 0; 
            $total_count= count($caption_array);

            $result_final .= "<div class='prevnext'>"; 
            $result_final .= "<span class='prev'><a href=/gallery.php?cname=$cname&pcaption=".$caption_array[$next]."><img src=/photos/assets/left.png  border=0 ></a></span>"; 
            $result_final .= "<span class='next'><a href=/gallery.php?cname=$cname&pcaption=".$caption_array[$prev]."><img src=/photos/assets/right.png  border=0 ></a></span>"; 
            $result_final .= "</div>"; 
        }            
    }

   $cname = str_replace(" ", "-", $cname);

   $images_dir =str_replace(".","",$images_dir);

   $result_final .= "<div class=limage><table><tr><td><table class=image><tr>\n\t<td><a href=/gallery.php?cname=$cname&pcaption=".$caption_array[$next]."><img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_keywords."' /></a>
        <div class=caption>".$photo_caption."</div> 
        <div class='excerpt'>".$photo_description."</div> 
        </td>                    
        </tr></table></td></tr></table><div class=underline></div></div>
        <!-- .limagePage --></div>  ";
}

在使用前定義它以避免此警告

$caption_array = array();

在上面定義while($ row = mysql_fetch_array($ result)){...

$caption_array = array();

在while循環之前

while ($row = mysql_fetch_array($result)) { 

您的錯誤可能有多種原因。 讓我來引導您。

  1. 檢查兩個變量名是否匹配,也許您輸入錯了
  2. 檢查您的include ,您可能會錯過定義該變量的那個
  3. 確保您沒有在某個時候取消設置
  4. 您是在函數中定義變量,還是試圖從函數中訪問變量? 確保使用global關鍵字。 請參見下面的代碼。

// Set variable
$var = 'test';

function func() {
    var_dump($var); // NULL, perhaps errors may occur

    global $var; // The magic trick
    var_dump($var); // "test"
}

func();

這是雙向的

function func() {
    global $var; // The magic trick
    $var = 'test'; // Set variable
}

var_dump($var); // NULL, perhaps errors may occur
func();
var_dump($var); // "test";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM