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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:45:26+00:00 2026-05-24T16:45:26+00:00

I am using PDO to connect with my MySQL db So before in my

  • 0

I am using PDO to connect with my MySQL db

So before in my users list i did:

  • Build the query by conditions (online? new user? sex?)
  • prepared the query
  • bound the values to the query
  • execute
  • fetch in a while() to show the results

Now I am just calling a object, UserCollection, load it and foreach the output.

$list = new UserCollection( $connect );
$list->load();

like that. In load() is the standard query for now:

    $stmt = $this->_pdo->query( 'SELECT id FROM users' );
    $data = $stmt->fetchAll( PDO::FETCH_ASSOC );

Theres no bounded variables to it, so it is not prepared.

Works fine, and it grabs just all the users.

Now what I wish to do is pass the query, that has been build to UserCollection() and use it to load() in the query.

I could do this easy, if the query was not bounded to variables, like the query above.

So what should I do if i want to pass a variable like this:

SELECT firstname, lastname, id, sex, last_access, bostadsort FROM users WHERE sex=:sex

Then i would need to bind :sex, and i cant just write bindValue() as

  1. the value is not inside the UserCollection object
  2. sometimes it could be only WHERE firstname=:firstname and not :sex, so it would throw error that I have bound a value that I dont use..

So what should I do here? what can i do?

Thanks in forward

  • 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-24T16:45:27+00:00Added an answer on May 24, 2026 at 4:45 pm

    You could modify your load method:

    function load($params = array()){
        $stmt = $this->_pdo->prepare("*your sql here*");
        if (!empty($params)){
            foreach ($params as $param_key=>$param_value)
                 $stmt ->bindParam($param_key, $param_value);
        }
        $data = $stmt->fetchAll( PDO::FETCH_ASSOC );
    }
    

    this way you don’t have to modify existing code. To use it with new queries with arbitrarry number of parameters you’ll just call it like:

    $sex = 'male';
    $firstname = 'Richard';
    $params = array(':sex'=>&$sex, ':firstname'=>&$firstname);
    $list->load($parameters);
    

    Don’t have PHP at hand, so can’t test it, but I think that should work.

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

Sidebar

Related Questions

I'm using this code and I'm beyond frustration: try { $dbh = new PDO('mysql:dbname='
I'm using PDO to connect to a mysql database. I'm confused as to where
I'm using PDO for connect to the database in a system where I want
I'm using PDO to insert a record (mysql and php) $stmt->bindParam(':field1', $field1, PDO::PARAM_STR); $stmt->bindParam(':field2',
I am using PDO with PHP to create a new database and then a
while using the following constructor $this->pdo = new PDO ($this->source, $this->username, $this->password); if I
I have been trying to connect to my database using pdo on godaddy with
Can I connect to SQL Server 2008 using PDO and integrated security using the
I'm using PDO inside a database abstraction library function query that I've made. I'm
Using PDO / PHP and MySQL, how can I check if a record was

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.