Why select limit doesn't work in php7?

why mysql select limit variable doesn’t work in php7. I set “bindParam” & “bindValue” but it is not working.

pleaze solve this problem. My code is here…

<?php
$dsn = 'mysql:dbname=example_12345678_bcs;host=sql112.example.com';
$user = 'user_12345678';
$password = 'mypassword';
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
$limit = 1;
$sql = "SELECT * FROM content ORDER BY id DESC LIMIT :limit";
try {
  $stmt = $dbh->query($sql);
  $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
  $result = $stmt->setFetchMode(PDO::FETCH_NUM);
  while ($row = $stmt->fetch()) {
?>
  <div><?php  print $row[2]; ?> </div>
  <div><?php  print $row[3]; ?> </div><br/>
  <div><?php  print $row[4]; ?> </div><br/>
  <div><?php  print $row[5]; ?> </div><br/>
<hr/>
<?php
  }
}
catch (PDOException $e) {
  print $e->getMessage();
}
?>

@soheljaman4 I’ve temporarily unlisted you thread, as it appears to contain all of your login credentials. Can you check this out and remove anything that might compromise your security?

bindValue() and bindParam() don’t work on regular queries.

If you had PDO’s error reporting enabled, it would have told you the query were invalid.

1 Like

I edited the sensitive data, as it’s past the time where the OP can edit his own post.

@soheljaman4: please be careful in future, and remember to remove any sensitive information from your posts. Information in these forums is publicly available, and your log-in details could be harvested by bots.

3 Likes

You need first to prepare query using PDO::prepare method
Then you need to PDO::bindParam or PDO::bindValue
Then you need to execute it with PDO::execute

I highly recommend you to read PDO Tutorial. - currently best PDO tutorial online

And this from same article related to LIMIT: A problem with LIMIT clause.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.