Php 使用sprintf命令、限制和查询

Php 使用sprintf命令、限制和查询,php,mysql,Php,Mysql,我在SQL查询中包含顺序和限制时遇到问题 $STH_1 = $DBH_R->query("SELECT table_name FROM information_schema.tables WHERE table_name

我在SQL查询中包含顺序和限制时遇到问题

$STH_1 = $DBH_R->query("SELECT table_name                        
                         FROM information_schema.tables                        
                        WHERE table_name                        
                         LIKE 'v_c_ei\_9%'
                         ");
$stmts_1 = array();

//echo "date = ".       $date."<br>";                                 // $date is today date
$date_60 = date('Y-m-d', strtotime('-60 day', strtotime($date)));     // $date_60 = today - 60 days


while (($row_1 = $STH_1 -> fetch_assoc()) !== null){

$table_name = $row_1['table_name'];
$stmts_1[] = sprintf("SELECT *  
                        FROM $table_name
                       WHERE (date_time >= '$date_60') AND (ei_code = '1117')     
                     ");     
} 

// at this place I need help, I think. I have few data from every query but I want to reduce the number of solutions to 1 per table

$stmt_1 = implode("\nUNION\n", $stmts_1);  
$stmt_1 .= "\nORDER BY date_time ASC";      
$STH_5_2 = $DBH_R->query($stmt_1);        
while (($row_5_2 = $STH_5_2 -> fetch_assoc()) !== null){
在sprintf查询中,但它不想工作。当我添加订单等。我有答案

致命错误:在“while($row_5_2…”中对非对象调用成员函数fetch_assoc()


任何人都可以提供帮助?

查看您的代码,您可能会遇到以下错误(但请检查以确保):


当您通过sprintf查询进行尝试时,您的代码到底是什么样子的?您检查过mysql中的错误吗?@MarkByers可能什么都没有,因为他似乎在使用mysqli。请尝试
echo$DBH\u R->error;
这个带有UNION和ORDER的部分工作正常。我必须在UNION之前选择接收数据,并且我尝试按照我写的那样做(使用sprintf查询中的订单)并且此订单不想工作…:(答案很简单->ORDER BY never before UNION always after.马克-理论上你的解决方案是可以的,但在这个问题上不行-我们不知道我们有多少表-从第一次查询开始就必须这样做…马克,谢谢你的帮助…但我记得你的评论你删除了哪一个。有时新医生会更好…@Andrew:“ORDER BY never before UNION always after”这不是一个正确的语句。您可以在联合之前或之后使用ORDER BY。但它的含义取决于您放置它的位置。如果您在联合之前使用ORDER BY,则需要在每个select语句
(select…ORDER BY…)周围加上括号UNION…
。通常,只有当您在订单上加上一个限制时,您才会这样做。
ORDER BY date_time DESC LIMIT 0,1
Incorrect usage of UNION and ORDER BY
(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);