我正在使用PHP和MySQLi编写复杂的数据库查询。
free
php最终将打印SQL字符串。当我完全采用此字符串并将其复制并粘贴到phpMyAdmin SQL输入字段时,它将返回单个结果行
$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 mysqli_query对于完全相同的查询字符串返回零行。
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;
返回0,不显示任何数据。这是一个示例,其中当我搜索的关键字为“ matching”时,查询应仅返回一个条目。如果我使用不同的关键字,它将返回多行,但总是比phpMyAdmin小一行。
这很奇怪!你知道这里发生了什么吗?
答案 0 :(得分:0)
尝试执行此查询,您需要将'{caracter}作为操作删除caracter and replace
,然后将type>'4'转换为type> 4,并确保$ searchstring的开头和结尾分别为>
$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)) {
...
}