1
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

8
  • 4
    It might be that your error reporting level is different on localhost compared to web server. Commented Jul 22, 2012 at 10:13
  • That might be it, but have you checked that your local database has all the columns in place? Commented Jul 22, 2012 at 10:14
  • are you sure that you've set up mysql correctly on localhost? Commented Jul 22, 2012 at 10:15
  • Can there be a problem in future if i ignore this notice? I did not initialize each and every index of the $cast array, is that okay? Commented Jul 22, 2012 at 10:16
  • @madfriend Yeah very sure my server and local host have the exactly same database Commented Jul 22, 2012 at 10:17

4 Answers 4

1

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

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

Comments

0

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! ;)
}

1 Comment

No, the result is getting retrieved and echoed but I'm getting warnings
0

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.

2 Comments

Data is being fetched from the database and echoed properly. The warnings are bothering me
array 'id' => array 0 => string '1' (length=1) 'role' => array 0 => string 'Action' (length=6) 'role_name' => array 0 => string 'MAN' (length=3) 'is_approved' => array 0 => string '0' (length=1) 'movie_id' => array 0 => string '252YZ5gY34' (length=10)
0

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.

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.