0

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

2
  • In SELECT * FROM dbname limit 1 is "dbname" just a typo for your example here? That should be the table you are selecting from. Commented Mar 26, 2012 at 8:33
  • Are you sure you really mean to select * from dbname which seems to be your database name, not a table? Commented Mar 26, 2012 at 8:34

6 Answers 6

3

Your current code will actually never get around to querying the database because of this typo:

if(!$con){  // MEANT TO WRITE $con

Since the connection is always going to be good (otherwise there's a die in the previous line that would have stopped the script -- which actually makes the condition redundant) this condition will always fail and $PlayList will 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?

Sign up to request clarification or add additional context in comments.

3 Comments

isset returns false, so it is printing out the error message he mentioned. (The echo line).
@Ilion: "but i`m getting the error var is undefined" => this is what I 'm referring to. It's obvious that he would get the "something went wrong" message (the question says so), but he's not asking about that.
I just figured his question was oddly worded and the final statement before his code sample explained what he was really receiving. Nothing else made sense otherwise.
2

Either use $PlayList or $Playlist, but not mixed.

Variables are case-sensitive.

Comments

1

something seems wrong with :

  $result = mysql_query("SELECT * FROM dbname limit 1");  

dbname should be table name please check. Make sure your query is returning results try to run at console.

and also initilize $PlayList array before while loop :

like : $PlayList=array();

php variables are case-sensitive. Please check they should be in same case.

if(isset($PlayList)){
    echo $PlayList."<br />";
    return $PlayList;
} else {
    echo "something went wrong while quering";
}

Comments

0

Your code is ok, except one thing:

if(isset($PlayList)){
    echo $PlayList."<br />";
    return $PlayList;
} else {
    echo "something went wrong while quering";
}

note that $PlayList and $Playlist are not same :)

Comments

0

Database selection can be done only if DB connection is effective:

$con = mysql_connect("localhost", "user", "pass") or die(mysql_error()); 
if($con !== FALSE) {
  mysql_select_db("dbname") or die(mysql_error());
...

Comments

0

PHP is case-sensitive about variable's names, but not about function/method names.

Because of that PHP "see" $PlayList and $Playlist as 2 diff vars.

Also, check if the $result is valid resource:

$result = mysql_query("SELECT * FROM table_name limit 1");
if( $result )
    while($row = mysql_fetch_array($result))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.