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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:34:36+00:00 2026-06-03T07:34:36+00:00

I used to have one function to any select query in my script, but

  • 0

I used to have one function to any select query in my script, but since it’s not secure I need to find out how to make a dynamic PDO class in php.

For example, I used to use this code.

function excute_query ($Query)
{

    if($Query_Run = mysql_query($Query))
    {
        while($DATA = mysql_fetch_array($Query_Run, MYSQL_ASSOC))
            {
                $data_array[] = $DATA;
            }
            return $data_array;
            close();

    }
    else{
            die(mysql_error());
        }

    mysql_close();

}

and I can use it anywhere in my script and it return’s an array with the data. in 2 lines.

$sql = 'SELECT * FROM users';
$users = execute_query($sql);

Now I need to use the same function with PDO. I’ve been trying, but how I can make a class or function to do the same using PDO.

What I’m trying to say. instead of write same 4 lines to make a query, is there any way to make a function or class take a query and return the data ?

Thank you.

  • 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-03T07:34:37+00:00Added an answer on June 3, 2026 at 7:34 am

    A quick and dirty but little verbose solution would be this one

    function execute_query( $query, $params ) {
        global $pdo;
        $stmt = $pdo->prepare( $query );
        $stmt->execute($params);
        return $stmt->fetchAll();
    }
    $users = execute_query('SELECT * FROM users WHERE id=?',array($id));
    

    but personally I hate writing the word array, so, I would use func_get_args() and call this way

    function execute_query() {
       global $pdo;
       $args  = func_get_args();
       $query = array_shift($args);
       $stmt  = $pdo->prepare($query);
       $stmt->execute($args);
       return $stmt->fetchAll();
    }
    $sql   = 'SELECT * FROM users WHERE username=? AND password=?';
    $found = execute_query($sql,$username,$password);
    

    But having a class would be apparently better.
    It will let you have different functions for the different result sets, like

    $username = db::getOne('SELECT name FROM users WHERE id=?',$id);
    

    instead of your current

    $user     = execute_query('SELECT name FROM users WHERE id=?',$id);
    $username = $user['name'];
    

    or, even more useful ones

    $users = db::getIndexed('id','SELECT name FROM users);
    

    to get an array where keys filled with user ids or

    $ids = db::getCol('SELECT id FROM users');
    $in  = implode(",",$ids);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used the query.next() function inside my code, but its returning false. I
I have one javascript function that used to display the menu and content in
I have used a join query for retrieving value from two tables one is
Question ONE: I'm still pretty new to .net, but have used Visual Studio for
I have used a jQuery script and some php code to generate two select
I used to have one class for one file. For example car.cs has the
I have created one application in j2me, I have used json parsing in that
i have done self study on learning struts 2. i have used one code
The problem: we have one application that has a portion which is used by
I'm used to the Java model where you can have one public class per

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.