Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 求救:关于这段代码中取数据的问题!!

求救:关于这段代码中取数据的问题!!

Jun 23, 2016 pm 02:13 PM

本帖最后由 XiaoShengbboy 于 2013-06-14 00:42:16 编辑

向各位求救
$sql="select * from xym_pic";
$res=mysql_query($sql,$conn);
while ($row=mysql_fetch_assoc($res)){
$data[]=$row;
}// 随机抽取9条记录以模拟实际情况
$keys = array_rand($data, 10);
$json = array();
foreach($keys as $key){
$json[] = $data[$key];
}
echo json_encode( $json );
这段代码中瀑布流data.php部分的代码,每次获取一次,就从数据库里面取出数据 放到数组$data[]里面,原来的是随机去取9条记录,我想让这段编程,每次获取去除按时间排序的9条记录,就是第一次取出来的是0-9,第二次是10-9,以此类推!那位能帮忙告诉怎么取出数据呢?万分感谢啊!小弟奉上100分,解决了另加!!!如果好心人原意加QQ指导,那是相当的感激啊:QQ 2 7 7 6 2 8 1 8 1

回复讨论(解决方案)

session_start();if(! isset($_SESSION['last'])) $_SESSION['last'] = 0;$last = $_SESSION['last'];$sql="select * from xym_pic limit $last,9";$res=mysql_query($sql,$conn);while ($row=mysql_fetch_assoc($res)){  $data[]=$row;}$_SESSION['last'] += count($data);echo json_encode( $data )
Copy after login
Copy after login
Copy after login
;

感谢版主的回复 已经解决了 谢谢谢谢!!!!!!!!!

session_start();if(! isset($_SESSION['last'])) $_SESSION['last'] = 0;$last = $_SESSION['last'];$sql="select * from xym_pic limit $last,9";$res=mysql_query($sql,$conn);while ($row=mysql_fetch_assoc($res)){  $data[]=$row;}$_SESSION['last'] += count($data);echo json_encode( $data )
Copy after login
Copy after login
Copy after login
;

我还有一个小问题,就是我数据库里面一共有 80多条记录,每次取出来九条,取出来8次,就取完了,但是我刷新页面的时候session还在,我就取不出来数据了,请问怎么解决一下呢?谢谢啊


session_start();if(! isset($_SESSION['last'])) $_SESSION['last'] = 0;$last = $_SESSION['last'];$sql="select * from xym_pic limit $last,9";$res=mysql_query($sql,$conn);while ($row=mysql_fetch_assoc($res)){  $data[]=$row;}$_SESSION['last'] += count($data);echo json_encode( $data )
Copy after login
Copy after login
Copy after login
;

我还有一个小问题,就是我数据库里面一共有 80多条记录,每次取出来九条,取出来8次,就取完了,但是我刷新页面的时候session还在,我就取不出来数据了,请问怎么解决一下呢?谢谢啊
瀑布流应该也不会用session存放吧?应该是和分页一样通过请求参数确定数据起始吧

取不到了就从头再取

取不到了就从头再取
现在就是因为取不到了,我判断如果结果集是空的话就销毁session,刷新页面还是取不出来数据


取不到了就从头再取
现在就是因为取不到了,我判断如果结果集是空的话就销毁session,刷新页面还是取不出来数据

结果集为空的话 我让session销毁了,但是他又会无限循环了
我想结果集为空了,就不取结果了,直到页面刷新,才重新计数
请问怎么解决呢

session_start();if(! isset($_SESSION['last'])) $_SESSION['last'] = 0;$last = $_SESSION['last']; $sql="select * from xym_pic limit $last,9";$res=mysql_query($sql,$conn);if(mysql_num_rows($res) ==0) {  $last = $_SESSION['last'] = 0;  $sql="select * from xym_pic limit $last,9";  $res=mysql_query($sql,$conn);}while ($row=mysql_fetch_assoc($res)){  $data[]=$row;}$_SESSION['last'] += count($data);echo json_encode( $data )
Copy after login



取不到了就从头再取
现在就是因为取不到了,我判断如果结果集是空的话就销毁session,刷新页面还是取不出来数据

结果集为空的话 我让session销毁了,但是他又会无限循环了
我想结果集为空了,就不取结果了,直到页面刷新,才重新计数
请问怎么解决呢
这个 代码最后的结果集 json是返回给一个JS文件的,js判断页面位置不停的加载新内容,我即使销毁了session,一加载也会重新开始获取数据!
这个很是头疼啊!

你究竟想要做什么?

你究竟想要做什么?
就是这个瀑布流,我想按条取出来最新的80条数据,然后就终止了,我刷新页面,再重新取数据!
现在这个瀑布流的机制是,静态页面调用js,js控制页面当前位置,距离底部多少个px之后获取php文件,这段php代码没问题,就是js获取的时候,是不是得判断一下如果json为空就不在获取呢?

现在是js只要滚动就会不断的获取内容,如果php里面结果集为空,他在获取一次也就会有数据了!

取完了就注销掉 session 或设置 $_SESSION 为空

js 那边当然也需要是否有数据返回,没有的话就不再发请求

取完了就注销掉 session 或设置 $_SESSION 为空

js 那边当然也需要是否有数据返回,没有的话就不再发请求

十分感谢啊,我把JS搞一下试试!
感谢您的耐心帮助哦!!!!!!!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO) How do you prevent SQL Injection in PHP? (Prepared statements, PDO) Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

See all articles