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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:07:36+00:00 2026-05-27T09:07:36+00:00

I have the following code : sub run_query { my $name = shift ||

  • 0

I have the following code :

sub run_query { 
    my $name = shift || undef;
    my $sql  = (defined $name ) ? "select * from table where name = ?" :
                                  "select * from table";
    my $sth  = $dbh->prepare("$sql");
    $sth->execute($name);
}

The above subroutine need to work as follows: if $name is provided, then run the first query, else fetch all the data from the table. How can I bind the name field? I’d like it bound dynamically if it is provided.

  • 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-27T09:07:37+00:00Added an answer on May 27, 2026 at 9:07 am

    From the DBI documentation on cpan:

    A common issue is to have a code fragment handle a value that could be
    either defined or undef (non-NULL or NULL) at runtime. A simple
    technique is to prepare the appropriate statement as needed, and
    substitute the placeholder for non-NULL cases:

      $sql_clause = defined $age ? "age = ?" : "age IS NULL";
      $sth = $dbh->prepare(qq{
        SELECT fullname FROM people WHERE $sql_clause
      });
      $sth->execute(defined $age ? $age : ());
    

    It does not exactly apply to your question, which I assume is that your execute fails if you add an argument where one is not expected. So, the last line here would apply:

      $sth->execute(defined $name ? $name : ());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: Private Sub SortWorksheet(ByVal sheet As Worksheet) Dim sStartColumn Dim
i have the following code Private Sub select_color_Click(ByVal sender As System.Object, ByVal e As
(resolved: see bottom) I have the following code snippet: Protected Sub SqlDataSource1_Inserted(ByVal sender As
I have the following code in my class : sub new { my $class
I have the following code: Imports MySql.Data.MySqlClient Imports MySql.Data.Types Public Class FormMain Private Sub
Okay so i have a semi weridish problem with re.sub. Take the following code:
I have code like the following in a UserControl: Protected Overrides Sub Render(ByVal writer
I have code, similar to the following, that I would like to modify: Sub
I have the following code which works FANTASTIC. Except - It is a sub-query
I have the following code: Private Sub btnReserve_Click(ByVal sender As Object, ByVal e As

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.