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 6056269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:21:27+00:00 2026-05-23T08:21:27+00:00

Using php and mysql 5.x. I currently load a banner image in a certain

  • 0

Using php and mysql 5.x. I currently load a banner image in a certain section of my site like so:

SELECT * FROM banners WHERE section = 1 AND pageid = 2

But if no results found I run a second query:

SELECT * FROM banners WHERE section = 1 AND pageid = 0

Basically what Im doing is trying to find banner images assigned to that section for that page. If no results found then I look for any default banner images in the second query. Is there a better way where I can do this in one query?

EDIT

To clarify a little bit more. I want to check if there is any banners assigned to the page, if not then see if there is any banners assigned to 0 (Default). I dont want a mix of both either it shows all banners assigned to that page or show all banners assigned to pageid 0 and there could be a possibility of multiple rows returned not just one.

ADDITIONAL EDIT

To better explain what this is for. In an admin tool I allow someone to assign a banner image to section on the website. In the admin tool they can select the section and the page they want the banner image to show. They can also set the default banner image(s) for that section. So if there were no banner images assigned to a section by default it will load the banner image(s) assigned to 0 for that section throughout the website. So instead of assigning a default banner image to 50 different pages they can just do it one time and it will load the default banner image or images for that section. Just trying to find a way to do this in a more optimal way, instead of 2 queries could it be done in one?

  • 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-05-23T08:21:28+00:00Added an answer on May 23, 2026 at 8:21 am

    Maybe it’s not the most elegant way, but you can try this:

    SELECT * 
    FROM banners 
    WHERE section = 1 
    AND pageid = IF(
        (select count(*) from banners where section = 1 and pageid = 2) = 0, 
        0, 2
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using PHP, JAVASCRIPT, MYSQL, XHTML, CSS to develop my site. Note that
I currently am using mysql and php to display 9 random results from my
I am currently using PHP/MySQL, and I'd like to know the best way make
I'm designing a website using PHP and MySQL currently and as the site proceeds
I am currently using PHP to paginate my data from mysql. I want to
I am currently using PHP/MySQL and the jQuery timeago plugin. Basically, I need to
I'm currently developing a website which stores bookmarks in a MySQL database using PHP
Using PHP and MySQL, I am integrating a site with a wiki based on
I am trying to get Ajaz to pull data from mysql using a php
I am currently developing an ecommerce software using PHP/MySQL for a big company. There

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.