简体   繁体   中英

Undefined variable: id PHP

Can every explain or show me why i'm getting this Undefined variable: id.

The function

public function getByEmbedId($id, $lang=null){
    $id = mysql_real_escape_string($id);
    $embed = array();
    $e = mysql_query("SELECT * FROM movie_embeds WHERE id='$id'") or die(mysql_error());
    if (mysql_num_rows($e)>0){
        $movie = $this->formatEmbedData(mysql_fetch_assoc($e), $lang);
    }
    return $embed;
}

And calling the function.

<?php

$embed = $movie->getByEmbedId($id,$language);  
if (empty($embed)){
    $embed = '';
} else { 
    $smarty->assign("embed",$embed);
}

?>

If someone can help me out it would be most appreciated

Check your $id whether it is set or not before calling your function. Do like this

<?php
if(isset($id)) // Make use of the isset construct here
{
$embed = $movie->getByEmbedId($id,$language);  
if (empty($embed)){
    $embed = '';
} else { 
    $smarty->assign("embed",$embed);
}
}
?>

when you call $embed = $movie->getByEmbedId($id,$language); , are $id and $language variables defined before ?

在函数中使用变量$ id之前,应先声明该变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM