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

  • Home
  • SEARCH
  • 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 8660963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:23:01+00:00 2026-06-12T16:23:01+00:00

I have the following code in php to query the database based on an

  • 0

I have the following code in php to query the database based on an array sent by the client.

$limit = $_POST['limit'];
$userArray = json_decode($_POST['arr'], true);
$queryPlaceholders= implode(',', array_fill(0,count($userArray), '?'));
$stmt = $db->prepare("SELECT * FROM tableA
                          WHERE tableA.id IN (".$queryPlaceholders.")
                          LIMIT ?");
foreach($userArray as $k => $val){
    $stmt->bindParam(($k+1), $val);
}
$stmt->bindValue(count($userArray) + 1, (int)trim($limit), PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo $result;

This code appears to have an error. If I send an array containing the values 11 & 17, the query seems to only run with the value 17, not both 11 and 17.

If I print_r($userArray) I get Array
(
[0] => 11
[1] => 17
)

so I know php has the correct array. However, running this query with the code above, and running the query below yields different answers:

SELECT * FROM tableA
WHERE tableA.id IN (11,17)
LIMIT 10

When running the code above, It appears to infact run this query?

SELECT * FROM tableA
WHERE tableA.id IN (17)
LIMIT 10

I’ve also placed statements in the foreach loop that tells me both elements of the array (11 & 17) are being bound to the $stmt

  • 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-12T16:23:02+00:00Added an answer on June 12, 2026 at 4:23 pm

    The problem is that you are using bindParam():

    Binds a PHP variable to a corresponding named or question mark placeholder in the SQL statement that was use to prepare the statement. Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.

    Since $val is changed on each iteration of the foreach loop, it ultimately is the same for each placeholder when the query is finally executed.

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

Sidebar

Related Questions

i have the following piece of code in php: define('QUERY', $some_query_string); if(empty(QUERY) || mb_strlen(QUERY)
I have the following code validation php script: if(empty($_POST['captcha_code'])) { $error = 1; $code[3]
I have the following code within a file named facebook.php: $user = json_decode(@file_get_contents( 'https://graph.facebook.com/me?access_token='
i have the following code: <!--ajouter une carte --> <?php if($_POST[submit_dd]){ mysql_query(INSERT INTO data
I have the following php code: $seasons = array(Autumn, Winter, Spring, Summer); Now I
In my Zend Framework app, I have following code: index.php $app_started_time = time(); layout.phtml
I have following PHP code $val=<div id=user.$row['cid']. userid=.$row['cid']. class=innertxt><img src=images/images.jpg width=50 height=50><strong>.$uname.</strong><ul> <li>Email: .$row['cemail'].</li>
I have the following PHP code, and for the life of me I can't
I have the following php code: $k=1; for($i=0; $i < 5; $i++) { $stmt
I have the following php code: $k=1; for($i=0; $i < 5; $i++) { $stmt

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.