Php mysqli未正确读取AND/OR语句
我在从数据库检索数据时遇到问题,mysqli忽略了代码中的一些AND语句Php mysqli未正确读取AND/OR语句,php,database,datetime,select,mysqli,Php,Database,Datetime,Select,Mysqli,我在从数据库检索数据时遇到问题,mysqli忽略了代码中的一些AND语句 $query_check = "select * from databaseTime where (authID='".$Database->real_escape_string($_SESSION['profile']['authID'])."' AND userID = '".$Database->real_escape_string($_POST['contact'])
$query_check = "select * from databaseTime where
(authID='".$Database->real_escape_string($_SESSION['profile']['authID'])."' AND
userID = '".$Database->real_escape_string($_POST['contact'])."') AND (
fromDateTime >= '".$Database->real_escape_string($_POST['fromDate'].' '.$fromTime)."' AND
fromDateTime <= '".$Database->real_escape_string($_POST['toDate'].' '.$toTime)."') OR
(fromDateTime <= '".$Database->real_escape_string($_POST['fromDate'].' '.$fromTime)."' AND
fromDateTime >= '".$Database->real_escape_string($_POST['toDate'].' '.$toTime)."') OR
(toDateTime >= '".$Database->real_escape_string($_POST['fromDate'].' '.$fromTime)."' AND
toDateTime <= '".$Database->real_escape_string($_POST['toDate'].' '.$toTime)."'))";
$result_check = $Database->query( $query_check );
$row_check = $result_check->fetch_object();
if(!$row_check)
echo valid;
else
echo invalid;
UPDATE根据评论中的建议更改了代码中的几行:
这是我现在唯一的问题:
===========================================================================
user ID: 5 ------------------|.......................|---------------- base
user ID: 5 ------------------------------------------|.............|-- valid
不是打印有效的,而是给我一个无效的
当我从查询中删除(=)时,这就是问题所在
===========================================================================
user ID: 5 ------------------|.......................|---------------- base
user ID: 5 ------------------|.......................|---------------- invalid
不是打印无效,而是给我一个有效的。那么你想得到的是一个用户id和一堆点和破折号?除了你,没有人确切知道那是什么。似乎您正在尝试使用
基
获取不重叠的日期和时间。但这只是我的猜测,而你似乎是在以最复杂的方式做这件事。MySQLi正在完全按照你所写的方式阅读声明。。。。问题不在于MySQLi,而在于您的语句和其中的大括号,但当您使用MySQLi时,为什么不准备语句并使用绑定变量。。。。事实上,这会使陈述本身更容易阅读。是的,马克也是这么说的。如果您在任何其他mysql扩展中运行相同的查询,您将得到相同的结果。这是因为不包含或,因此如果满足其中任何条件,它将获得该数据
===========================================================================
user ID: 5 ------------------|.......................|---------------- base
user ID: 5 ------------------|.......................|---------------- invalid