1

I'm trying to convert mysqli_query to PDO.

I got stuck with my dynamically created WHERE clausule. I read this post (PHP PDO dynamic WHERE clause), but there you lose the prepare function of PDO, and that is what I want to do.

This is (part of) my origional code

// this is the '$_POST' array
$_POST['org'][0] = 1;
$_POST['org'][1] = 2;
$_POST['org'][2] = 5;

/* Execute a prepared statement by passing an array of values */
$sql['select'] = "SELECT * FROM `table` ";

if(isset($_POST['org']) AND count($_POST['org']) > 0) {  
  $sql['where'] .= "WHERE ( `org_id` = '".mysqli_real_escape_string($conn, $_POST['org'][0])."' ";

  foreach ($_POST['org'] as $key => $value) {
    if($key !== 0)
      $sql['where'] .= "OR `org_id` = '".mysqli_real_escape_string($conn, $value)."' ";
  }
}

$sql = $sql['select'] . $sql['where'];
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

How can I use prepared statements in the foreach loop?

2 Answers 2

1

You should build your query and instead of using values inside the query you should use placeholders (? or :placeholder_name):

$query = 'SELECT * FROM table WHERE col IN (?,?,?)';
$stmt = $pdo->prepare($query);
$stmt->execute($_POST['org']);

Go and read https://www.php.net/manual/en/pdo.prepare.php#refsect1-pdo.prepare-examples for more examples

Sign up to request clarification or add additional context in comments.

Comments

1

Ah!

This is what I did now, and it works. Thanks.

$sql['where'] .= "WHERE ( `org_id` = ? ";
$values[] = $_POST['org'][0];

foreach ($_POST['org'] as $key => $value) {
  if($key !== 0) {
    $sql['where'] .= "OR `org_id` = ? ";
    $values[] = $value;
  }
}

$sql['where'] .= ")";

$sql = $sql['select'] . $sql['where'];

$stmt = $pdo->prepare($sql);
$stmt->execute((array) $values);

Thank you!

4 Comments

Technically true but using named placeholders and building an associative array makes this a lot more flexible and is only slightly more work.
a note on style: my preference is to include the required leading space before the WHERE or OR along with the keyword, rather than requiring preceding code to include an extra trailing space at the end. e.g. .= " WHERE ... and .= " OR ... Just makes more sense to me. When looking at the code that sets up the first part of the select, it's not at all obvious why a trailing space is needed.But looking at the code that appends the WHERE, its more obvious why there needs to be space before the WHERE keyword,... seems like we should do it there, rather than backtrack to other code
@tadman what do you mean? Can you give a little example of that?
If you use named placeholders that match your args, you can just execute($_POST) or execute($_POST['org']) or whatever. You should also consider org_id IN (?, ?, ...) instead of org_id=? OR org_id=? OR .... The answer by d0niek incorporates both these elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.