简体   繁体   English

仅在使用限制时显示php和mysql查询结果

[英]php and mysql query results displayed only if using limit

In an attempt to learn more php, mysql & json, I downloaded a demo database from: http://www.mysqltutorial.org/wp-content/uploads/downloads/2013/05/mysqlsampledatabase1.zip . 为了尝试了解更多php,mysql和json,我从http://www.mysqltutorial.org/wp-content/uploads/downloads/2013/05/mysqlsampledatabase1.zip下载了一个演示数据库。

I want to display all customers to echo them out in html table format with edit and delete buttons. 我想显示所有客户,以带有编辑和删除按钮的html表格式回显他们。

My SQL statement is 我的SQL语句是

"SELECT * FROM customers" 

It returns no results and no errors. 它没有返回结果,也没有错误。 However, if I change the SQL statement to 但是,如果我将SQL语句更改为

"SELECT * FROM customers LIMIT 11" 

The first 11 of 122 rows are returned. 返回122行中的前11行。 Using a LIMIT of 1-11 will return results while anything 12 or more returns noting. 使用1-11的LIMIT将返回结果,而12或更多的结果将返回结果。

Can anyone shed some light on this for me please? 有人可以帮我阐明一下吗?

UPDATE: Code for assistance. 更新:寻求帮助的代码。

 <?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "classicmodels"; $dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password) or die(mysql_error()); $sql = 'SELECT * FROM customers LIMIT 12' or die(mysql_error()); $stmt = $dbh->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode($result); die(); ?> 

Thank you in advance, 先感谢您,

Bg BG

The problem is not in your SQL code. 问题不在您的SQL代码中。 If 如果

SELECT * FROM customers LIMIT 11

works correctly, so will 正常工作,所以会

SELECT * FROM customers LIMIT 12

Therefore, there is probably an error in your php code. 因此,您的php代码中可能存在错误。 Either it doesn't handle the 12th record retrieved correctly, or it cannot handle the 12+ limit for some other reason. 它要么不能处理正确检索到的第12条记录,要么由于其他原因不能处理12条以上的限制。 Could you expand the question with the relevant php code? 您能用相关的php代码扩展问题吗?

Edit 编辑

To correctly display special characters, such as the å in Luleå (your 12th record), you must match the character set in the database settings, database connection and output. 若要正确显示特殊字符,例如Luleå中的å(您的第12条记录),您必须匹配数据库设置,数据库连接和输出中的字符集。

For example, if your database collation is UTF-8, make your first query in your script SET NAMES 'utf8' . 例如,如果数据库排序规则为UTF-8,则在脚本SET NAMES 'utf8'进行第一个查询。 Then, before printing the json, send a header to the browser (I'm assuming you're outputting to a browser): header('Content-Type: text/json; charset=utf-8'); 然后,在打印json之前,将标头发送到浏览器(假设您要输出到浏览器): header('Content-Type: text/json; charset=utf-8');

By configuring your database correctly, the SET NAMES becomes obsolete. 通过正确配置数据库,SET NAMES会过时。

Some helpful guides I found explain this in more detail: 我发现一些有用的指南对此进行了更详细的说明:

Since you are selecting everything from the table, you may want to change your syntax to: 由于您正在从表中选择所有内容,因此您可能需要将语法更改为:
SELECT * FROM customers (rather then @) 选择*来自客户(而不是@)
LIMIT x LIMIT x
Where x is your number, this should allow for numbers beyond 12. 其中x是您的数字,这应允许数字超过12。

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

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