简体   繁体   English

避免使用php进行多个MySql查询

[英]Avoid multiple MySql queries using php

I'm trying to select both the total number and the limited number of products from the database. 我正在尝试从数据库中选择产品总数和有限数量。

Example: 例:

$result = $database->query("SELECT* FROM products WHERE type = $category limit $start,$per_page");

$all_data = $database->query("SELECT* FROM products WHERE type = $category");

However, when I run this I'm getting mysql error. 但是,当我运行此程序时,出现mysql错误。 Is it possible to get the data I need without using multiple queries. 是否可以在不使用多个查询的情况下获取我需要的数据。

This is mysql error I'm getting; 这是我得到的mysql错误;

Database failed...You have an error in your SQL syntax; 数据库失败...您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '-2,2' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的'-2,2'附近使用正确的语法

If I understand you correctly, you're fetching the entire set of products in your category in the second query, but fetching just one page's worth in the first query (eg, items 10 through 19). 如果我对您的理解正确,那么您将在第二个查询中获取类别中的整个产品集,但是在第一个查询中仅获取一个页面的价值(例如,项目10到19)。 I would just fetch all the items with the second query, then load the rows into a PHP array and use array_slice() to grab the segment of the array you need for the current page. 我只是用第二个查询获取所有项目,然后将行加载到PHP数组中,并使用array_slice()来获取当前页面所需的数组段。

EDIT: As others have said, the actual MySQL error may be the lack of the space between SELECT and * , but you can also do what you're trying to do without hitting the database twice. 编辑:正如其他人所说,实际的MySQL错误可能是SELECT*之间缺少空格,但是您也可以做您想做的事情而无需两次访问数据库。

If you just need the counts, then use: 如果您只需要计数,请使用:

SELECT count(*)
FROM products
WHERE type = '$category' limit $start,$per_page");

SELECT count(*)
FROM products
WHERE type = '$category';

The error is due to the use of negative numbers in limit clause. 该错误是由于在limit子句中使用了负数。 Snippet from MySQL documentation on Select syntax : MySQL文档中有关Select语法的摘录:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT子句可用于约束SELECT语句返回的行数。 LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements). LIMIT接受一个或两个数字参数,这两个参数都必须是非负整数常量(使用预处理语句时除外)。

So the resolution to that error would be to use prepared statements if you really need negative limits as also asked by @James in one of his comments on your question. 因此,解决该错误的方法是,如果您确实需要负限制,请使用准备好的语句,就像@James在他对问题的评论之一中所要求的那样。

Note that select* does not produce any errors but certainly does confuse! 请注意, select*不会产生任何错误,但肯定会造成混淆!

You create a procedure then you call this procedure. 创建一个过程,然后调用该过程。 I hope it work for you. 希望它对您有用。

CREATE PROCEDURE `test_proc`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
declare name1 TEXT;
declare id1 TEXT;
select name,id into name1,id1 from my_tbl WHERE name='sam';
select * from my_tbl;
select name1,id1;
END

You can call this single call store procedure. 您可以调用此单个呼叫存储过程。

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

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