douguomou5094 2018-06-22 08:20
浏览 54

php mysqli与phpmyadmin相比返回不同的行数

I'm writing a complicated database query using PHP and MySQLi.

   $SQLstring = "
        SELECT * FROM (SELECT `Index`, `Type`, `Date`, `User`, `Site`, `acronym`, `ShiftIndex`, `TaskName`, `Text` FROM `shiftentry` 
        INNER JOIN (SELECT `Index` AS `shiftIndex`, `Date`, `User`, `Site`, `Status` FROM `shiftreports`) `shiftreports` on `ShiftReport` = `shiftIndex`
        INNER JOIN (SELECT `Index` AS `siteIndex`, `acronym` FROM `sites`) `sites` ON `Site` = `siteIndex`
        INNER JOIN (SELECT `Index` AS `taskIndex`, `TaskName` FROM `tasks`) `tasks` on `Status` = `taskIndex`) AS a
        WHERE `Type` < '4' ".$searchstring."

        UNION

        SELECT * FROM (SELECT `Index`, `Type`, `Date`, `User`, `Site`, `acronym`, `ShiftIndex`, `TaskName`, `process`.`Text` AS `Text` FROM `shiftentry` 
        INNER JOIN (SELECT `Index` AS `shiftIndex`, `Date`, `User`, `Site`, `Status` FROM `shiftreports`) `shiftreports` on `ShiftReport` = `shiftIndex`
        INNER JOIN (SELECT `Index` AS `siteIndex`, `acronym` FROM `sites`) `sites` ON `Site` = `siteIndex`
        INNER JOIN (SELECT `Index` AS `taskIndex`, `TaskName` FROM `tasks`) `tasks` on `Status` = `taskIndex`
        INNER JOIN (SELECT `Index` AS `statusIndex`, `process`, `progress`, `Comment` FROM `site-status`) `site-status` on `shiftentry`.`Text` = `statusIndex`
        INNER JOIN (SELECT `Index` AS `processIndex`, `Processname` AS `Text` FROM `process`) `process` on `process` = `processIndex`) AS b
        WHERE `Type` = '4' ".$searchstring."

        UNION

        SELECT * FROM (SELECT `Index`, `Type`, `Date`, `User`, `Site`, `acronym`, `ShiftIndex`, `TaskName`, `site-status`.`Text` AS `Text` FROM `shiftentry` 
        INNER JOIN (SELECT `Index` AS `shiftIndex`, `Date`, `User`, `Site`, `Status` FROM `shiftreports`) `shiftreports` on `ShiftReport` = `shiftIndex`
        INNER JOIN (SELECT `Index` AS `siteIndex`, `acronym` FROM `sites`) `sites` ON `Site` = `siteIndex`
        INNER JOIN (SELECT `Index` AS `taskIndex`, `TaskName` FROM `tasks`) `tasks` on `Status` = `taskIndex`
        INNER JOIN (SELECT `Index` AS `statusIndex`, `process`, `progress`, `Comment` AS `Text` FROM `site-status`) `site-status` on `shiftentry`.`Text` = `statusIndex`) AS c
        WHERE `Type` = '4' ".$searchstring."

        ORDER BY `Date` DESC;";
    echo $SQLstring;
    $shiftentries=mysqli_query($conn, $SQLstring); 
    echo mysqli_num_rows($shiftentries);

    while($shiftentry = mysqli_fetch_array($shiftentries)) {
       ...
    }

The php is printing the SQL string in the very end. When I take exactly this string and copy&paste it to the phpMyAdmin SQL input field it will return a single result line

SELECT * FROM (SELECT `Index`, `Type`, `Date`, `User`, `Site`, `acronym`, `ShiftIndex`, `TaskName`, `Text` FROM `shiftentry` INNER JOIN (SELECT `Index` AS `shiftIndex`, `Date`, `User`, `Site`, `Status` FROM `shiftreports`) `shiftreports` on `ShiftReport` = `shiftIndex` INNER JOIN (SELECT `Index` AS `siteIndex`, `acronym` FROM `sites`) `sites` ON `Site` = `siteIndex` INNER JOIN (SELECT `Index` AS `taskIndex`, `TaskName` FROM `tasks`) `tasks` on `Status` = `taskIndex`) AS a WHERE `Type` < '4' AND `Text` LIKE '%matching%' UNION SELECT * FROM (SELECT `Index`, `Type`, `Date`, `User`, `Site`, `acronym`, `ShiftIndex`, `TaskName`, `process`.`Text` AS `Text` FROM `shiftentry` INNER JOIN (SELECT `Index` AS `shiftIndex`, `Date`, `User`, `Site`, `Status` FROM `shiftreports`) `shiftreports` on `ShiftReport` = `shiftIndex` INNER JOIN (SELECT `Index` AS `siteIndex`, `acronym` FROM `sites`) `sites` ON `Site` = `siteIndex` INNER JOIN (SELECT `Index` AS `taskIndex`, `TaskName` FROM `tasks`) `tasks` on `Status` = `taskIndex` INNER JOIN (SELECT `Index` AS `statusIndex`, `process`, `progress`, `Comment` FROM `site-status`) `site-status` on `shiftentry`.`Text` = `statusIndex` INNER JOIN (SELECT `Index` AS `processIndex`, `Processname` AS `Text` FROM `process`) `process` on `process` = `processIndex`) AS b WHERE `Type` = '4' AND `Text` LIKE '%matching%' UNION SELECT * FROM (SELECT `Index`, `Type`, `Date`, `User`, `Site`, `acronym`, `ShiftIndex`, `TaskName`, `site-status`.`Text` AS `Text` FROM `shiftentry` INNER JOIN (SELECT `Index` AS `shiftIndex`, `Date`, `User`, `Site`, `Status` FROM `shiftreports`) `shiftreports` on `ShiftReport` = `shiftIndex` INNER JOIN (SELECT `Index` AS `siteIndex`, `acronym` FROM `sites`) `sites` ON `Site` = `siteIndex` INNER JOIN (SELECT `Index` AS `taskIndex`, `TaskName` FROM `tasks`) `tasks` on `Status` = `taskIndex` INNER JOIN (SELECT `Index` AS `statusIndex`, `process`, `progress`, `Comment` AS `Text` FROM `site-status`) `site-status` on `shiftentry`.`Text` = `statusIndex`) AS c WHERE `Type` = '4' AND `Text` LIKE '%matching%' ORDER BY `Date` DESC;

phpMyAdmin query result

The php mysqli_query unfortunately returns zero lines for exactly the same query string.

echo mysqli_num_rows($shiftentries);

returns 0 and no data is displayed. This is an example where the query should just return a single entry when the keyword I'm searching for is "matching". In case I use different keywords it will return multiple lines but always one line less than phpMyAdmin.

It's strange! Do you have any idea what's going on here?

  • 写回答

1条回答 默认 最新

  • doupin1073 2018-06-22 09:00
    关注

    Try this query you need to remove caracter and replace with ' caracter for as action and turn type>'4' to type>4 and make sure $searchstring have a and in first:

    $SQLstring = "
        SELECT * FROM (SELECT Index, Type, Date, User, Site, acronym, ShiftIndex, TaskName, Text FROM shiftentry 
        INNER JOIN (SELECT Index AS 'shiftIndex', Date, User, Site, Status FROM shiftreports) shiftreports on ShiftReport = Index
        INNER JOIN (SELECT Index AS 'siteIndex', acronym FROM sites) sites ON Site = Index
        INNER JOIN (SELECT Index AS 'taskIndex', TaskName FROM tasks) tasks on Status = Index) AS a
        WHERE Type < 4 ".$searchstring."
    
        UNION
    
        SELECT * FROM (SELECT Index, Type, Date, User, Site, acronym, ShiftIndex, TaskName, process.Text AS Text FROM shiftentry 
        INNER JOIN (SELECT Index AS 'shiftIndex', Date, User, Site, Status FROM shiftreports) shiftreports on ShiftReport = Index
        INNER JOIN (SELECT Index AS 'siteIndex', acronym FROM sites) sites ON Site = Index
        INNER JOIN (SELECT Index AS 'taskIndex', TaskName FROM tasks) tasks on Status = Index
        INNER JOIN (SELECT Index AS 'statusIndex', process, progress, Comment FROM site-status) site-status on shiftentry.Text = Index
        INNER JOIN (SELECT Index AS 'processIndex', Processname AS Text FROM process) process on process = Index) AS b
        WHERE Type = 4 ".$searchstring."
    
        UNION
    
        SELECT * FROM (SELECT Index, Type, Date, User, Site, acronym, ShiftIndex, TaskName, site-status.Text AS 'Text' FROM shiftentry 
        INNER JOIN (SELECT Index AS 'shiftIndex', Date, User, Site, Status FROM shiftreports) shiftreports on ShiftReport = Index
        INNER JOIN (SELECT Index AS 'siteIndex', acronym FROM sites) sites ON Site = Index
        INNER JOIN (SELECT Index AS 'taskIndex', TaskName FROM tasks) tasks on Status = Index
        INNER JOIN (SELECT Index AS 'statusIndex', process, progress, Comment AS Text FROM site-status) site-status on shiftentry.Text = Index) AS c
        WHERE Type= 4 ".$searchstring."
    
        ORDER BY Date DESC;";
    echo $SQLstring;
    $shiftentries=mysqli_query($conn, $SQLstring); 
    echo mysqli_num_rows($shiftentries);
    
    while($shiftentry = mysqli_fetch_array($shiftentries)) {
       ...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序