Im trying to make a function that retrieves a value from the db, but i`m getting the error var is undefined. I have read that you could set the php error level lower, but that looks like bad practice to me.
I have tried to pass de undefined var as param, I have tried to give it a standard value. but I dont seem to understand the concept here.
here is the code: $Playlist is undefined, something went wrong is printed to the screen
function returnPlaylistIndex(){
$con = mysql_connect("localhost", "user", "pass") or die(mysql_error());
if(!$con){
mysql_select_db("dbname") or die(mysql_error());
$result = mysql_query("SELECT * FROM dbname limit 1");
while($row = mysql_fetch_array($result))
{
$PlayList = $row['playlistIndex'];
}
mysql_close($con);
}
if(isset($Playlist)){
echo $Playlist."<br />";
return $Playlist;
} else {
echo "something went wrong while quering";
}
}
any help is appreciated
Your current code will actually never get around to querying the database because of this typo:
Since the connection is always going to be good (otherwise there’s a
diein the previous line that would have stopped the script — which actually makes the condition redundant) this condition will always fail and$PlayListwill never be set.Apart from that, note that you are using two separate styles of naming for the variable:
$PlayList(camel case) and$Playlist(uppercase first letter only).That said, I still do not understand why you would get an error message in this function since you are actually testing with
isset. Can you provide the actual error and point out the line in which it occurs?