简体   繁体   English

如何在PHP中自动增加mysql Limit命令

[英]How to auto increment mysql Limit command in php

i need to display mysql table rows one at a time for this i am using in php: 我需要一次在php中显示一个mysql表行:

$result = mysqli_query($con,"select * from table limit 0,1");
while($row = mysqli_fetch_array($result)) {
    echo "First Name:" . $row['First'] . " " . "Last Name:" . $row['Last'];
    echo "<br>";
}

I need to display one row at a time, but display every new row on every php executinon, like first user gets to see first "firstname, lastname" second anonymous user on executing the page gets to see second "firstname, lastname" from the database. 我需要一次显示一行,但是在每个php可执行文件上显示每一行,就像第一个用户在执行页面时看到的是第一个“名字,姓氏”,第二个匿名用户在执行该页面时看到的是第二个“名字,姓氏”。数据库。

this gives an error : 这给出了一个错误:

$result = mysqli_query($con,"select * from voucher limit '$count',1");

(i included $count to the parameters) (我将$ count包含在参数中)

On execution it just displays the first row every time. 执行时,每次仅显示第一行。 I need modification to it that whenever the code is executed it returns a new row value. 我需要对其进行修改,以便无论何时执行代码,它都将返回新的行值。 For example, first time it will output first row, on seond execution of the script it outputs second row and so on.. 例如,第一次将输出第一行,第二次执行脚本时将输出第二行,依此类推。

i am using php5, mysql on ubuntu server. 我在ubuntu服务器上使用php5,mysql。

Please help. 请帮忙。

You can do it in two ways : The first one is to save the increment value somewhere. 您可以通过两种方式做到这一点:第一种是将增量值保存在某处。 I dont know how do you pass it or when and how you exsecute it, but it they run run separately each time (eg runs via a cron job or terminal), you can save the value in a simple file or memchache. 我不知道您如何通过它或何时以及如何对其进行检举,但是它们每次都分别运行(例如,通过cron作业或终端运行),您可以将值保存在一个简单的文件或memchache中。

First way : 第一种方式:

$offset = file_get_contents('my_counter.txt');
$offset++;
$result = mysqli_query($con,"select * from table limit " . (int) $offset . ",1");
file_put_contents('my_counter.txt', $offset);

The next one is to save the the last id from your row: 下一个是保存行中的最后一个ID:

$lastID = file_get_contents('my_counter.txt');
if($lastID == "") // First call and no last id
    $lastID = 0;

$result = mysqli_query($con,"select * from table WHERE ID > " . (int) $lastID . " limit 0,1");
while($row = mysqli_fetch_array($result)) {
     echo "First Name:" . $row['First'] . " " . "Last Name:" . $row['Last'];
     $lastID = $row['ID']; // Your autoincrement row
}
file_put_contents('my_counter.txt', $lastID);

I hope I understood your question right and this helps you. 希望我对您的问题理解正确,这对您有所帮助。

Try this 尝试这个

$result = mysqli_query($con,"select * from table ORDER BY RAND() LIMIT 1");
while($row = mysqli_fetch_array($result)) {
   echo "First Name:" . $row['First'] . " " . "Last Name:" . $row['Last'];
echo "<br>";
}

Since you want the data to be retrived when php code runs, you need to count the loop inside your code and increment the value like below. 由于您希望在运行php code时检索数据,因此需要计算代码内的循环并按如下所示递增值。

If you want to loop, you need to increment the 0 in LIMIT 0,1 如果要循环,则需要在LIMIT 0,1增加0

Something like this in a loop 像这样的循环

LIMIT " . $loopcount . ",1
LIMIT " . $loopcount . ",1
LIMIT " . $loopcount . ",1

Im not a php programmer so i dont know the exact code. 我不是一个PHP程序员,所以我不知道确切的代码。

You could if you just need a random and not the next, use SELECT * FROM table ORDER BY RAND() LIMIT 1 . 如果您只需要一个随机数而不是下一个,则可以使用SELECT * FROM table ORDER BY RAND() LIMIT 1

This just gives a random record, and you wont go outside of the table as it always selects one record from the table. 这只是给出一个随机记录,您也不会走出表格,因为它总是从表格中选择一条记录。

It will affect your performance a bit if you have a big table. 如果您有一张大桌子,它会稍微影响您的性能。 But you will always get a record. 但是,您将始终获得记录。 If you just loop LIMIT " . $loopcount . ",1 you run the chance of trying to select outside of the table scope. 如果仅循环LIMIT " . $loopcount . ",1 ,则有机会尝试在表范围之外进行选择。 If you have 100 rows, and you try LIMIT 101,1 you will get a empty recordset. 如果您有100行,并且尝试使用LIMIT 101,1 ,则将获得一个空的记录集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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