0

Here is the piece of code:

  $stmt1 = $conn->prepare("SELECT * FROM image_table WHERE username=:mname && status=1 ORDER BY number DESC LIMIT :min, 5");
  $stmt1->bindParam(':mname', $mname);
  $stmt1->bindParam(':min', $minimum, PDO::PARAM_INT);
  $stmt1->execute();

This code is inside a loop that increases $minimum by 5 each time it runs starting at 0, to display all of a members images in rows of 5 columns. When I set :min manually it works but I think because it changes with each iteration, it won't display anything. Does bindParam not work with a dynamic variable?

Justin
  • 11
  • 1
    You can `prepare` once and `execute` any number of times with different binds. Why not try that approach? – tadman Mar 22 '16 at 00:48
  • How could I accomplish that? I'm still not too familiar with prepared statements – Justin Mar 22 '16 at 01:03
  • You've got all the code you need right here. Just repeat the last three lines, but leave the first outside your loop. – tadman Mar 22 '16 at 01:05
  • Ah, gotcha. I'll give it a try. Thank you very much! – Justin Mar 22 '16 at 01:22
  • If you do get it working, add the code as an answer. That'll help someone else in the same situation. That someone could even be you in the future if you forget how to do it which has happened to all of us at some point! – tadman Mar 22 '16 at 01:23
  • I managed to get it working perfectly by using intval($min) in place of :min; `$stmt = $conn->prepare("SELECT * FROM smf_image WHERE username=:mname && status=1 ORDER BY image_number DESC LIMIT ".intval($min).", 5");` – Justin Mar 22 '16 at 03:03
  • It's probably safer to [follow this advice](http://stackoverflow.com/questions/30389723/php-pdo-error-when-using-placeholders-in-the-limit-clause-of-a-mysql-query) and keep it as a placeholder. Use `intval()` in the call to `bindParam`. – tadman Mar 22 '16 at 03:07
  • I'll take a loot at that too. Thanks again for you help! – Justin Mar 22 '16 at 03:09
  • What version of MySQL? – Rick James Jun 30 '22 at 19:34

0 Answers0