简体   繁体   中英

Limiting the number of SQL results

SO basically I have a load.php page that receives a position variable and uses it to display 6 results from that position from the table... but I see that there's a mistake somewhere because nothing gets returned.... can you guys please help ?

Here is the code :

  <?php 


error_reporting(0);
session_start();
include '../upload/connect.php';

$start = $_POST['start'];
$id = $_POST['id'];

$sql = mysql_query("SELECT * FROM comments WHERE id='".$id."' ORDER by id DESC LIMIT ".$start." , 6 ") or die(mysql_error());
while ($display = mysql_fetch_assoc($sql))
{
    ?>
    <div id="comments">
    <table>
    <tr>
    <td rowspan="2"><img src="../pic/logo.png" width="100px" /></td>
    <td valign="top"><p style="width:700px;font-family:'Lucida Sans Unicode', 'Lucida Grande', sans-serif;font-size:13px;color:rgba(255,255,255,0.5);">&nbsp;&nbsp;&nbsp;&nbsp;Postat de <?php echo $display['user']; ?> la <?php echo $display['date']; ?> </p></td></td>
    </tr>
    <tr>
    <td width="90%" valign="top"><p style="width:700px;font-family:'Lucida Sans Unicode', 'Lucida Grande', sans-serif;font-size:13px;color:white;">&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $display['comment']; ?></p>
    </tr>
    </table>
    </div>
    <?php
}


?>

And jquery:

var st = 6;// start position...
    var div_height = $("#mighty_holder").height()/2- 50;
    var doc_scroll = $(document).scrollTop();
    function loadthem (k)
    {
        $.post('../core/load.php',{start: k , id: <?php echo json_encode($id); ?>},
                function(result){
                    $("#comment_holder").append(result);
            }); 
    }

    $(document).scroll(function(){
        if ($("#mighty_holder").height()/2- 50 < $(document).scrollTop())
        {
            loadthem(st);   
            st = parseInt(st) + 7;
        }
    });

In your Javascript code, you initialize var st = 6 send 6 to start . Then in the scroll function it is passed as parameter to the loadthem function resulting in 6 being send as start: k in the AJAX POST request to the PHP page. It recovered as $start = $_POST['start'] and inserted in the query like 6 resulting in LIMIT 6, 6 . Which will not return any result from the query.

You probably meant to initialize st to 0 .

On a side note: to prevent SQL injections I suggest you to perform an int cast on both $_POST['id'] and $_POST['start'] , assuming they should be both numeric values:

$start = (int) $_POST['start'];
$id = (int) $_POST['id'];

This is your mistake

var st = 6;// start position...

st should be 0. In your case you have LIMIT 6, 6 . I am guessing you want LIMIT 0, 6

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