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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:28:17+00:00 2026-05-10T23:28:17+00:00

I have an array full of random content item ids. I need to run

  • 0

I have an array full of random content item ids. I need to run a mysql query (id in the array goes in the WHERE clause), using each ID that’s in the array, in the order that they appear in the said array. How would I do this?

This will be an UPDATE query, for each individual ID in the array.

  • 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. 2026-05-10T23:28:18+00:00Added an answer on May 10, 2026 at 11:28 pm

    As with nearly all ‘How do I do SQL from within PHP’ questions – You really should use prepared statements. It’s not that hard:

    $ids  = array(2, 4, 6, 8);  // prepare an SQL statement with a single parameter placeholder $sql  = 'UPDATE MyTable SET LastUpdated = GETDATE() WHERE id = ?'; $stmt = $mysqli->prepare($sql);  // bind a different value to the placeholder with each execution for ($i = 0; $i < count($ids); $i++) {     $stmt->bind_param('i', $ids[$i]);     $stmt->execute();     echo 'Updated record ID: $id\n'; }  // done $stmt->close(); 

    Alternatively, you can do it like this:

    $ids    = array(2, 4, 6, 8);  // prepare an SQL statement with multiple parameter placeholders $params = implode(',', array_fill(0, count($ids), '?')); $sql    = 'UPDATE MyTable SET LastUpdated = GETDATE() WHERE id IN ($params)'; $stmt   = $mysqli->prepare($sql);  // dynamic call of mysqli_stmt::bind_param                    hard-coded eqivalent $types = str_repeat('i', count($ids));                        // 'iiii' $args = array_merge(array($types), $ids);                     // ['iiii', 2, 4, 6, 8] call_user_func_array(array($stmt, 'bind_param'), ref($args)); // $stmt->bind_param('iiii', 2, 4, 6, 8)  // execute the query for all input values in one step $stmt->execute();  // done $stmt->close(); echo 'Updated record IDs: ' . implode(',' $ids) .'\n';  // ---------------------------------------------------------------------------------- // helper function to turn an array of values into an array of value references // necessary because mysqli_stmt::bind_param needs value refereces for no good reason function ref($arr) {     $refs = array();     foreach ($arr as $key => $val) $refs[$key] = &$arr[$key];     return $refs; } 

    Add more parameter placeholders for other fields as you need them.

    Which one to pick?

    • The first variant works with a variable number of records iteratively, hitting the database multiple times. This is most useful for UPDATE and INSERT operations.

    • The second variant works with a variable number of records too, but it hits the database only once. This is much more efficient than the iterative approach, obviously you can only do the same thing to all affected records. This is most useful for SELECT and DELETE operations, or when you want to UPDATE multiple records with the same data.

    Why prepared statements?

    • Prepared statements are a lot safer because they make SQL injection attacks impossible. This is the primary reason to use prepared statements, even if it is more work to write them. A sensible habit to get into is: Always use prepared statements, even if you think it’s ‘not really necessary.’ Neglect will come and bite you (or your customers).
    • Re-using the same prepared statement multiple times with different parameter values is more efficient than sending multiple full SQL strings to the database, because the database only needs to compile the statement once and can re-use it as well.
    • Only parameter values are sent to the database on execute(), so less data needs to go over the wire when used repeatedly.

    In longer loops the execution time difference between using a prepared statement and sending plain SQL will become noticeable.

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

Sidebar

Ask A Question

Stats

  • Questions 65k
  • Answers 65k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer CUDA is probably a better option, if you know your… May 11, 2026 at 11:04 am
  • added an answer Yes you should enter into a contract, you need to… May 11, 2026 at 11:04 am
  • added an answer This code compiles and runs under GCC with -Wall. #include… May 11, 2026 at 11:04 am

Related Questions

I have an array full of random content item ids. I need to run
I have an array in Perl: my @my_array = (one,two,three,two,three); How do I remove
I have an array of numbers that potentially have up to 8 decimal places
I have an array of a few million numbers. double* const data = new
I have an array of items that are time sensitive. After an amount of
I have an array of hashes, and I want the unique values out of
I have an array of 1000 strings to load into a combo box. What
I have an array I've created in JavaScript. The end result comes out to
I have an array of shorts (short[]) that I need to write out to
I have an array of integers: int[] number = new int[] { 2,3,6,7 };

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.