简体   繁体   中英

SQL LIMIT query not woking

I have the following PHP code:

<?php
  include 'DBConnect.php';
  $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
  $query = "SELECT * FROM telejoke.jokes LIMIT 2";
  $data = mysql_query($query) or die('Error, insert query failed' . mysql_error());
  $info = mysql_fetch_array( $data ); 
  mysql_close($conn);
  echo json_encode($info);   
?>

For instance, if I put LIMIT 2, I want only the first 2 rows from the table to be gathered, encoded into a JSON Array and echoed. Regardless of this LIMIT number, echo json_encode($info); it prints out the whole table.

Trying echo json_encode($data); results in null output.

Help is appreciated. Thanks.

Try this...

$info = array();
while ($row = mysql_fetch_array($data)) {
  $info[] = $row;
}

echo json_encode($info);

trying doing this:

SELECT * FROM telejoke.jokes LIMIT 0,2

or

SELECT * FROM jokes LIMIT 0,2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM