小编典典

如何在 PHP 代码数据库输出中添加查询参数

php

我有tasks.php显示数据库所有结果的文件。

我现在希望在 URL 中添加查询参数,这将导致“tasks.php/query=books”只显示原始数据,这些数据显示列中Task_titletask_description我的 SQL 数据库中包含“books”的数据。

说如果query=""然后返回所有任务,query=somevalue然后根据它进行过滤。

我已经尝试成功地为搜索查询创建一个单独的文件,但我需要在这个 tasks.php 中包含应用程序开发,我不确定如何集成。

这是我的tasks.php

<?php

 // Create connection
$con=mysqli_connect("test","test","test","test");

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Select all of our stocks from table 'stock_tracker'
$sql = "
SELECT * FROM `tasks` ORDER BY `tasks`.`date_added` DESC

";

$cValue=str_replace(array("'",",","."), array("&amp;#039;","&amp;#44;","&amp;#46;"), $_POST['contractValue']);

// Confirm there are results
if ($result = mysqli_query($con, $sql))
{
    // We have results, create an array to hold the results
    // and an array to hold the data
    $resultArray = array();
    $tempArray = array();

    // Loop through each result
    while($row = $result->fetch_object())
    {
        // Add each result into the results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    // Encode the array to JSON and output the results

echo json_encode($resultArray, DEFINED('JSON_INVALID_UTF8_IGNORE') ? JSON_INVALID_UTF8_IGNORE : 0);


}

// Close connections
mysqli_close($con);
?>

阅读 60

收藏
2022-07-25

共1个答案

小编典典

步骤1。

在 Url 中应该是 - tasks.php?query=books 而不是 tasks.php/query=books

第2步。

获取您的查询值表单 url

$search_key = $_GET['query'];

步骤 3。

更改您的选择查询。

SELECT * FROM `tasks` WHERE `Task_title` LIKE '%$search_key%' OR `task_description`  LIKE '%$search_key%' ORDER BY `tasks`.`date_added` DESC
2022-07-25