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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:46:06+00:00 2026-06-11T09:46:06+00:00

using PDO, I receive 1+ rows and turn them into an array like this:

  • 0

using PDO, I receive 1+ rows and turn them into an array like this:

while($row = $car_sales->fetch(PDO::FETCH_ASSOC)){          
    $car_id_array[] = $row["car_id"];
    $car_type_array[] = $row["car_type"];
    $dealer_id_array[] = $row["dealer_id"];
    $buyer_id_array[] = $row["seller_id"];
}

I’m trying to simply “mix” each level of the array to act as a unit and go down the foreach loop together, submitting the $q query in order. In other words, something like this:

foreach($dealer_id_array as $dealer_id) {
    if ($car_type=='new') {
        if ($dealer_id==$buyer_id){
          $q = 'UPDATE car_sales SET new_cars=new_cars+1 WHERE dealer_id=:dealer_id';
        } else if ($dealer_id!=$buyer_id){
          $q = 'UPDATE car_sales SET new_cars=new_cars-1 WHERE dealer_id=:dealer_id';
    } else if ($car_type=='old') {
        if ($dealer_id==$buyer_id){
          $q = 'UPDATE car_sales SET old_cars=old_cars+1 WHERE dealer_id=:dealer_id';
        } else if ($dealer_id!=$buyer_id){
          $q = 'UPDATE car_sales SET old_cars=old_cars-1 WHERE dealer_id=:dealer_id';
    }
    $car_update = $dbhandle->prepare($q);
    $car_update->execute(array(':dealer_id' => $dealer_id));
}

The loop should run with the first array values if there is only one value retrieved from the while loop. If there are more, the foreach should run as many times as there are dealer_ids from the while loop, while respecting the order. This example won’t work, but I’m looking for possible solutions to solve this issue correctly. What do you think would be the most efficient way to do this?

  • 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-11T09:46:07+00:00Added an answer on June 11, 2026 at 9:46 am

    I might be misunderstanding you, but I believe if you just replaced your foreach loop with a for loop, and counted one of the arrays you would solve your issue:

     for($i = 0; $i < count($dealer_id_array); $i++) {
    

    and then simply using $i to access the correct item in your arrays:

    $car_type_array[$i];
    

    But having said that, I don’t really see the point of the initial four arrays in the first place. Could you not just directly execute the code inside the foreach loop straight inside the while loop in the first place?

    EDITED: to fix code example

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

Sidebar

Related Questions

I'm using PDO's fetch_class feature to retrieve data from my database row into object,
I am using PDO and want to do something like this: $query = $dbh->prepare(SELECT
I'm having some problems while using PDO. It all seems correct but I am
Can someone confirm this: If I am using PDO::PARAM, I don't need to Filter
I have written a function using PDO Prepare Statement to fetch columns from a
Using SQLite in PHP (thus using PDO), I have this code: try { $db
I'm using PDO in my application. But I have a problem while I'm working
I'm using PDO::query to run the following SQL statement: INSERT INTO pages (template_id,user_id,page_default,page_internal_title,page_menu_text,page_nav_link,globalcontent_id,page_parent_id,page_order,page_active,page_show_in_menu,page_hide,page_created,page_updated,page_deleted,page_type) values
I have run into a problem using PDO because an error was not caught.
I'm new to using PDO and prepared statement so if I'm doing this blatantly

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.