Abhijith
Abhijith

Reputation: 411

PHP code works on server but throws errors in localhost

for($i=0;$cast_row = mysql_fetch_array($res);$i++)
{
    $cast['id'][] = $cast_row['user_id'];
    $cast['role'][] = $cast_row['role'];
    $cast['role_name'][] = $cast_row['role_name'];
    $cast['is_approved'][] = $cast_row['is_approved'];
    $cast['movie_id'][] = $cast_row['movie_id'];
}
for($i=0;$i<count($cast['id']);$i++) //LINE 31
{
    $output .= "<tr>";
    $mname = getMovieNameById($m_id);
    $output .= "<td><a href='single.php?id=$m_id'>$mname</a></td>";

    $aname = getArtistNameById($cast['id'][$i]);
    $output .= "<td><a href=project.php?id={$cast['id'][$i]}>$aname</a></td>";
}

This code works fine in the web server but throws errors(notice) when executed on localhost

Notice: Undefined index: id in C:\wamp\www\Tinyflick\managemovie.php on line 31

What can be the problem? The rest of the code seems to work just fine

Upvotes: 1

Views: 194

Answers (4)

Abhijith
Abhijith

Reputation: 411

It kind of a silly mistake.. Its solved. Thanks to nico's comment

The error reporting level in localhost is different from the one on the server. Changing that will do the trick. The following code will show the all the errors and warnings

error_reporting(E_ALL)

Warnings are usually disabled on a production server. For more info refer the documentation of the said function

Upvotes: 1

Stano
Stano

Reputation: 8939

I guess that if you want supress those notices, add the following line before the loop:

$cast = array('id'=>array(), 'role'=>array(), 'role_name'=>array(), 'is_approved'=>array(), 'movie_id'=>array() );

to initialize the variable.

Upvotes: 0

Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

it turns out that $cast array is empty as you are not properly fetching the data from database.

if the error reporting is turned off in your server then you will not see any errors.

instead of for loop use the while loop, to iterate through the data fetched from database.

$cast = array();
while($row = mysql_fetch_array($res)) {
    $cast['id'][] = $row['user_id'];
    $cast['role'][] = $row['role'];
    $cast['role_name'][] = $row['role_name'];
    $cast['is_approved'][] = $row['is_approved'];
    $cast['movie_id'][] = $row['movie_id'];
}

and then you can run the for loop.

Upvotes: 0

easteregg
easteregg

Reputation: 509

i guess the mysql result is just empty ! :) try dumping the content of the row you get back.

btw, you could improove your code by doing something like this:

while($row = mysql_fetch_array($res)) { // iterates as long there is content
    //do something with the $row... like your second for block! ;)
}

Upvotes: 0

Related Questions