php mysqli返回的行数与phpmyadmin不同

ha5z0ras  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(289)

我正在使用php和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)) {
       ...
    }

php正在最后打印sql字符串。当我获取这个字符串并将其复制粘贴到phpMyAdminSQL输入字段时,它将返回一个结果行

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查询结果
不幸的是,对于完全相同的查询字符串,php mysqli\u查询返回零行。

echo mysqli_num_rows($shiftentries);

返回0,不显示任何数据。这是一个示例,当我搜索的关键字是“matching”时,查询应该只返回一个条目。如果我使用不同的关键字,它将返回多行,但总是比phpmyadmin少一行。
真奇怪!你知道这是怎么回事吗?

yzxexxkh

yzxexxkh1#

请尝试删除此查询 caracter and replace 使用'caracter for as action并将type>'4'转换为type>4,并确保$searchstring首先有一个and:

$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)) {
   ...
}

相关问题