Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Not get a return from database

    I am working Through a book Beginning with php5,apache,MySql,Web Development and AM getting this message can any one help.

    Warning: Division by zero in C:\xampp\htdocs\Creatingphp2\movie_details.php on line 67Bruse Almighty: Details
    Movie Title Year of Release Movie Director Movie Lead Actor Movie Running Time Movie Health Average Rating
    Bruse Almighty 2003 Tom Shadyac Jim Carrey 102 mins $10m


    Date of Review Review Title Reviewer Name Movie Review Comments Rating
    2003-08-02 This movie rocks! John Doe I thought this was a great movie even though my girlfriend made me see it against my will.
    2003-08-01 An okay movie Billy Bob This was an okay movie. I liked Eraserhead beter.
    2003-08-01 My favorite movie Marvin Marian I didn't wear flair to the movie but I loved it anyway.










    <?php
    $link = mysql_connect("localhost", "bp5am", "bp5ampass")
    or die(mysql_error());
    mysql_select_db("moviesite") or die (mysql_error());

    /*Function to calulate if a movie made profit,
    loss or broke even*/

    function calculate_differances($takings, $cost) {
    $difference = $takings - $cost;

    if ($difference<0) {
    $difference = substr($difference, 1);
    $font_color = 'red';
    $profit_or_loss = "$" . $difference . "m";
    } elseif (difference > 0) {
    $fontcolor ='green';
    $profit_or_loss = "$" . $difference . "m";
    } else {
    $font_color = 'blue';
    $profit_or_loss = "Broke even";
    }
    return "<font color =\"$font_color\">" . $profit_or_loss . "</font>";
    }
    /*Function to get the director's name from the people table*/
    function get_director(){
    global $movie_director;
    global $director;
    $query_d = "SELECT people_fullname FROM people WHERE people_id= '$movie_director'";
    $results_d = mysql_query($query_d)
    or die (mysql_error());
    $row_d = mysql_fetch_array($results_d);
    extract($row_d);
    $director = $people_fullname;
    }
    function get_leadactor() {
    global $movie_leadactor;
    global $leadactor;
    $query_a = "SELECT people_fullname " .
    "FROM people ".
    "WHERE people_id='$movie_leadactor'";
    $results_a = mysql_query($query_a)
    or die(mysql_error());
    $row_a = mysql_fetch_array($results_a);
    extract($row_a);
    $leadactor = $people_fullname;
    }


    function generate_ratings($review_rating) {
    $movie_rating = '';
    for($i=0; $i<$review_rating; $i++) {
    $movie_rating .= "<img src =\"thumbsup.gif\">&nbsp;";
    }
    return $movie_rating;
    }

    function avgreview(){
    global $avgreview;
    $avg_query =mysql_query("SELECT review_rating FROM reviews WHERE review_movie_id='" . $GET['movie_id'] . "'");
    $totalreviews = mysql_num_rows($avg_query);
    $current=0;
    while ($rows = mysql_fetch_array($avg_query)) {
    extract ($rows);
    $current = $current + $review_rating;
    }
    return $avgreview = $current / $totalreviews;
    }

    $movie_query = "SELECT * FROM movie WHERE movie_id ='".
    $_GET['movie_id'] . "'";

    $movie_result = mysql_query($movie_query, $link)
    or die(mysql_error());


    $review_query = "SELECT * FROM reviews WHERE review_movie_id ='" . $_GET['movie_id'] ."' ORDER BY review_date DESC";

    $review_result = mysql_query($review_query, $link)
    or die(mysql_error());




    $movie_table_headings=<<<EOD
    <tr>
    <th>Movie Title</th>
    <th>Year of Release</th>
    <th>Movie Director</th>
    <th>Movie Lead Actor</th>
    <th>Movie Running Time</th>
    <th>Movie Health</th>
    <th>Average Rating</th>
    </tr>
    EOD;

    $review_table_headings=<<<EOD
    <tr>
    <th>Date of Review</th>
    <th>Review Title</th>
    <th>Reviewer Name</th>
    <th>Movie Review Comments</th>
    <th>Rating</th>
    </tr>
    EOD;
    while($review_row = mysql_fetch_array($review_result)) {
    $review_flag =1;
    $review_title[] = $review_row['review_name'];
    $reviewer_name[] = ucwords($review_row['review_reviewer_name']);
    $review[] = $review_row['review_comment'];
    $review_date[] =$review_row['review_date'];
    $review_rating[] = generate_ratings($review_row['review_rating']);
    }

    $i =0;
    $review_details = '';
    while ($i<sizeof($review)){
    $review_details .=<<<EOD
    <tr>
    <td width="15%" valign="top" align="center">$review_date[$i]</td>
    <td width="15%" valign="top">$review_title[$i]</td>
    <td width="10%" valign="top">$reviewer_name[$i]</td>
    <td width="50%" valign="top">$review[$i]</td>
    <td width="10%" valign="top" align="center">$review_rating[$i]</td>
    </tr>
    EOD;
    $i++;
    }
    while ($row = mysql_fetch_array($movie_result)) {
    $movie_name =$row['movie_name'];
    $movie_director =$row['movie_director'];
    $movie_leadactor = $row['movie_leadactor'];
    $movie_year = $row['movie_year'];
    $movie_running_time = $row['movie_running_time']." mins";
    $movie_takings = $row['movie_taking'];
    $movie_cost = $row['movie_cost'];

    //get director's name from people table
    get_director();

    // get lead actor's name from people table
    get_leadactor();


    // get avg movie rating
    avgreview();

    };

    $review_query = "SELECT * from reviews ".
    "WHERE review_movie_id = '" . $_GET['movie_id'] ."' " .
    "ORDER BY review_date DESC";

    $review_result = mysql_query($review_query, $link)
    or die(mysql_error());

    $movie_health = calculate_differances($movie_takings, $movie_cost);
    $page_start =<<<EOD
    <html>
    <head>
    <title> Details and Reviews for: $movie_name</title>
    </head>
    <body>
    EOD;

    $movie_details =<<<EOD
    <table width= "70%" border="0" cellspacing= "2" cellpadding= "2" align="center">
    <tr>
    <th colspan="6".<u><h2>$movie_name: Details</h2></u></th>
    </tr>
    $movie_table_headings
    <tr>
    <td width= "33%" align="center">$movie_name</td>
    <td align="center">$movie_year</td>
    <td align="center">$director</td>
    <td align="center">$leadactor</td>
    <td align="center">$movie_running_time</td>
    <td align="center">$movie_health</td>
    <td align="center">$avgreview</td>
    </tr>
    </table>
    <br>
    <br>
    EOD;
    if ($review_flag) {
    $movie_details .=<<<EOD
    <table width="95%" border= "0" cellspacing= "2"
    cellpadding= "20" align= "center">
    $review_table_headings
    $review_details
    </table>
    EOD;
    }





    $detailed_movie_info =<<<EOD
    $page_start
    $movie_details
    $page_end
    EOD;
    echo $detailed_movie_info;
    mysql_close();
    ?>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I didn't count the lines, but it's probably here:
    return $avgreview = $current / $totalreviews;

    If there are no "totalreviews" ... you can't divide by zero.

    Maybe this will fix it?

    if($totalreviews > 0){
    return $avgreview = $current / $totalreviews;
    }
    else{
    return $avgreview = 0;
    }

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    mlseim is correct. It's basically like divide 0 by 0 what do you get? It can't be done.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    It's OK if $current is zero, but $totalreviews can't be zero.
    If it is zero, skip over the divide.

  • Users who have thanked mlseim for this post:

    tms848s (02-02-2010)

  • #5
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I will try this the database should have data in it

    THanks for your help I let you know


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •