Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:29:06+00:00 2026-06-14T11:29:06+00:00

Basically I have 2 methods in the same class, getMovie and getGenres. They are

  • 0

Basically I have 2 methods in the same class, getMovie and getGenres. They are very similar but One doesn’t return what I expect.

Here’s getMovie method:

public function getMovie($argType, $arg){
        $movieQuery =  "SELECT  id,
                                rt_id,
                                imdb_id,
                                url,
                                rt_url,
                                type,
                                adult,
                                DATE_FORMAT(release_date, '%Y') AS year,
                                date_added,
                                title,
                                runtime,
                                budget,
                                revenue,
                                homepage,
                                rating,
                                tagline,
                                overview,
                                popularity,
                                image,
                                backdrop,
                                trailer
                        FROM    movies
                        WHERE   " . $argType . " = " . $arg;

        $movieResult = $this->_query($movieQuery);
        $movies = array();

        if($movieResult->fetch_array(MYSQLI_ASSOC)){
            while($m = $movieResult->fetch_array(MYSQLI_ASSOC)){
                $movies[] = array(  'title' => $m['title'],
                                    'duplicate' => $m['duplicate'],
                                    'url' => $m['url'],
                                    'rt_url' => $m['rt_url'],
                                    'release_date' => $m['release_date'],
                                    'date_added' => $m['date_added'],
                                    'type' => 'movie',
                                    'adult' => $m['adult'],
                                    'id' => $id,
                                    'rt_id' => $m['rt_id'],
                                    'imdb_id' => $m['imdb_id'],
                                    'rating' => $m['rating'],
                                    'tagline' => $m['tagline'],
                                    'overview' => $m['overview'],
                                    'popularity' => $m['popularity'],
                                    'runtime' => $m['runtime'],
                                    'budget' => $m['budget'],
                                    'revenue' => $m['revenue'],
                                    'homepage' => $m['homepage'],
                                    'image' => $m['image'],
                                    'backdrop' => $m['backdrop'],
                                    'trailer' => $m['trailer'] );
            }
            return $movies;
        }
        else{
            return false;
        }

Here’s getGenres method:

public function getGenres($movieId = NULL){
        $genresQuery = "";
        if($movieId != NULL){
            $genresQuery = "SELECT  id,
                                    name
                            FROM    genres
                            WHERE   id = ANY (
                            SELECT  genre_id
                            FROM    movie_genres
                            WHERE   movie_id = " . $movieId . ")";
        }
        else{
            $genresQuery = "SELECT  id,
                                    name
                            FROM    genres";
        }
        $genresResult = $this->_query($genresQuery);
        $genres = array();

        if($genresResult->fetch_array(MYSQLI_ASSOC)){
            while($genre = $genresResult->fetch_array(MYSQLI_ASSOC)){
                $genres[] = array(  'id' => $genre['id'],
                                    'name' => $genre['name'] );
            }
            return $genres;
        }
        else{
            return false;
        }
    }

And here’s how I call them:

$mov = $movie->getMovie(2207);
print_r($mov); // output: Array()

$gen = $movie->getGenres(2207);
print_r($gen); // output: Array(values inside)

Both queries do actually return expected values but getMovies method doesn’t work with the if statement. It works fine if I just have while loop.

I am using if as well as while as I heard that while loop can sometimes execute even when there’s not values. Is there any truth to this? If there is indeed a reason to use an if statement as well as wile loop then why doesn’t it work with getMovies method?

Edit 1: I tried storing the array like so but that resulted in a memory related error:

$r = $genresResult->fetch_array(MYSQLI_ASSOC);
if($r){
            while($r){
                $genres[] = array(  'id' => $genre['id'],
                                    'name' => $genre['name'] );
            }
            return $genres;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:29:08+00:00Added an answer on June 14, 2026 at 11:29 am

    I am using if as well as while as I heard that while loop can sometimes execute even when there’s not values. Is there any truth to this?

    No, according to the php manual mysqli_result::fetch_array returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in resultset.

    Null is falsy so the while loop will not be entered.

    Although the if statement is unnecessary if you had one you would use mysqli_result::$num_rows to check if the query returned any rows.

    if($movieResult->num_rows > 0){
        while($m = $movieResult->fetch_array(MYSQLI_ASSOC)){
        ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I have a class with 2 methods: one to serialize an object into
I have two similar methods that basically does the same thing only with different
I have a class that basically has two methods the first one takes a
I have a static methods class, Utils, that is basically for utility methods, its
I want to test some methods that call others in the same class. They
I have two action methods that are conflicting. Basically, I want to be able
Basically, if I have a method (or several methods) that I want to expose
I have written a custom class for overriding the seek method of QSlider. Basically
I basically have a program that filters records from one excel file to another
I have a generic Collection class, with a variety of public getter methods. To

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.