Skip to content
Advertisement

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

Advertisement

Answer

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);
}
}
?>
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement