Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用prepared语句和explode php进行简单搜索查询_Php - Fatal编程技术网

使用prepared语句和explode php进行简单搜索查询

使用prepared语句和explode php进行简单搜索查询,php,Php,我正在尝试创建航班时刻表的搜索查询,但此查询似乎不起作用。我有一个表单,当用户搜索时,它执行一个查询以显示航空公司的航班时刻表。但对于trail,我只使用出发地点和到达地点。但我当前的搜索查询即使在数据库中也不会显示任何结果。下面是我的代码 mainsearch.php <?php error_reporting(E_ALL); require_once ("db.php"); $db = new MyDB(); if(isset($_POST['deptfrom']) &&

我正在尝试创建航班时刻表的搜索查询,但此查询似乎不起作用。我有一个表单,当用户搜索时,它执行一个查询以显示航空公司的航班时刻表。但对于trail,我只使用出发地点和到达地点。但我当前的搜索查询即使在数据库中也不会显示任何结果。下面是我的代码

mainsearch.php

<?php 
error_reporting(E_ALL); 
require_once ("db.php");
$db = new MyDB();

if(isset($_POST['deptfrom']) && isset($_POST['arrat']) && isset($_POST['deptdate']) && isset($_POST['retdate']) && isset($_POST['adults']) && isset($_POST['children']) && isset($_POST['infants']) && isset($_POST['ticket']))
{
$deptfrom = filter_input(INPUT_POST, 'deptfrom', FILTER_SANITIZE_STRING);
$arrat = filter_input(INPUT_POST, 'arrat', FILTER_SANITIZE_STRING);
$deptdate = htmlspecialchars($_POST['deptdate']);
$sword = explode(" ", $deptfrom);
$ssword = explode(" ", $arrat);
$retdate = htmlspecialchars($_POST['retdate']);
$adults = htmlspecialchars($_POST['adults']);
$children = htmlspecialchars($_POST['children']);
$infants = htmlspecialchars($_POST['infants']);
$ticket = filter_input(INPUT_POST, 'ticket', FILTER_SANITIZE_STRING);

if(empty($deptfrom) || empty($arrat) || empty($deptdate) || empty($retdate) || empty($adults) || empty($ticket))
{
    echo "fill";
    exit();
}
else 
{
    foreach($sword as $dat)
    {
        foreach($ssword as $aat)
        {
            $condition .= " location LIKE '%".SQLite3::escapeString($dat)."%' AND destination LIKE '%".SQLite3::escapeString($aat)."%' AND ";
        }
    }
    $condition = substr($condition, 0, -4);

    $query = "SELECT * FROM schedule WHERE " .$condition;
    $sql = $db->prepare($query);

    $result = $sql->execute();

    while($row = $result->fetchArray(SQLITE3_ASSOC))
    {
        $airline = $row['airline'];

        echo $airline;
    }    
}
}
?>


这个查询似乎没有给出任何错误或答案。有什么问题吗?非常感谢。

为了获得更好的结果,您应该检查错误。如果使用mysqli,则连接对象使用
$con->error

如果使用PDO,则使用

$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

尝试回显您的查询,看看它是否直接在数据库中工作。此外,您实际上并没有检查任何数据库错误。@aynber它
echo
直接sir@aynber我试图计算结果的数量,结果显示为零,即使它在数据库中也显示为零。请在此处显示您的回显查询,以便我们可以查看生成的查询。如果您在数据库中运行该精确查询,您会得到任何结果吗?在行
$query=“SELECT*FROM schedule WHERE”。$condition添加以下内容:
echo$query
然后将查询直接复制到数据库,查看是否得到结果