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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:04:09+00:00 2026-06-12T01:04:09+00:00

I want to conditionally build a SQL statement and am having issues. In this

  • 0

I want to conditionally build a SQL statement and am having issues. In this case, there may be a WHERE clause or not.

$sql=null;
$arr='';

if( $id != 'all' ){
    $sql = '  WHERE type = :type AND id=:id';
    $arr = array(':type'=>$type,':id'=>$id);
}

$stmt = $dbh->prepare("SELECT * FROM table $sql");
$stmt->execute($arr);
...

If the id is equal to ‘all’, then I just want to rewrite the sql without a WHERE clause.
Since I have to pass $arr to execute, I get the folowing error when id = ‘all’ because execute is expecting a value:
PHP Warning: PDOStatement::execute() expects parameter 1 to be array, string given

I’ve tried passing execute an empty array but get an error as well.
How do I do this without having to rewrite two complete blocks of sql, each with its own execute statememt?

  • 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-12T01:04:10+00:00Added an answer on June 12, 2026 at 1:04 am

    It looks like you need to move the prepare statement into the if section, adding a second prepare statement for the second case. It’d probably be better (for readability’s sake) if you also added an else for when id === 'all'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there a build-in mechanism to conditionally redirect to another view? I want the
Is there a good way to chain methods conditionally in Ruby? What I want
Is there a built-in asp.net way to conditionally serve pages, for example I want
In every webapp I build, I come across this issue. I want to set
I want to conditionally unhook an event handler. Is this the right way to
I want to conditionally output HTML to generate a page, so what's the easiest
I want to conditionally navigate to some page. If some condition is true i
In my code, I want to conditionally perform a few operations with: #ifdef DEBUG
I want to change content of a Button in XAML using a DataTrigger. Conditionally
I want to post in memory some child rows, and then conditionally post them,

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.