简体   繁体   English

显示来自 sql 查询的多个结果

[英]display multiple results from a sql query

so I am trying to display multiple results from a database when a query is searched, the query is passed from a search box on another page.所以我试图在搜索查询时显示来自数据库的多个结果,查询是从另一个页面上的搜索框传递的。

I have it displaying one result, but that is all it will display.我让它显示一个结果,但这就是它会显示的全部。 I need it to display all the results that are relevant to the search query.我需要它来显示与搜索查询相关的所有结果。

the php code is below php代码在下面

<meta charset="UTF-8">
<?php
    $mysqli = new mysqli('localhost', 'scott', 'tiger','courses');
    if ($mysqli->connect_errno) 
    {
        die('Database connection failed');
    }
    //$m->set_charset('utf8');
    $search_sql = "
    SELECT title, summary, id
    FROM course
    WHERE title LIKE '%".$_POST['searchBar']."%'";
    $result = $mysqli->query($search_sql) or die($mysqli->error);
    $search_result = $result->fetch_assoc();
?>
<!doctype html>
<head>
    <meta charset="utf-8">
    <h1>Search Results</h1>
</head>
<body>
    <h3><?= $search_result['title'] ?></h1>
    <p><?= $search_result['summary'] ?></p>
</body>

and the code for the search bar和搜索栏的代码

<!doctype html>
<html>
<Head>
    <meta charset = "utf-8">
    <title>Search</title>
</head>
<body>
    <h2>Search</h2>
    <form name="search" method="post" action="SearchResultsPage.php">
        <input name="searchBar" type="text" size="40" maxlength="60" />
        <input type="submit" name="Submitsearch" value="Search" />
    </form>
    </body>

Does anyone have any suggestions?有没有人有任何建议?

Thanks in advance;提前致谢;

You will need to place it in a while loop to show multiple results, the fetch function you're using will only retrieve one row, if you place it in a loop you can keep fetching until there is nothing to fetch:您需要将它放在一个 while 循环中以显示多个结果,您使用的 fetch 函数只会检索一行,如果您将它放在一个循环中,您可以继续获取直到没有任何内容可获取:

//$m->set_charset('utf8');
$search_sql = "
SELECT title, summary, id
FROM course
WHERE title LIKE '%".$_POST['searchBar']."%'";
$result = $mysqli->query($search_sql) or die($mysqli->error);
?>
<!doctype html>
<head>
    <meta charset="utf-8">
    <h1>Search Results</h1>
</head>
<body>
    <?PHP while($search_result = $result->fetch_assoc()) { ?>
    <h1><?= $search_result['title'] ?></h1>
    <p><?= $search_result['summary'] ?></p>
    <?PHP } ?>
</body>

PS your code is vulnerable to SQL injection, you should read about prepared statements. PS 您的代码容易受到 SQL 注入的影响,您应该阅读有关准备好的语句。 More Info on that 更多信息

You can iterate over your query results with a while loop.您可以使用 while 循环迭代查询结果。 To complete the example I added the necessary data cleaning.为了完成这个例子,我添加了必要的数据清理。

<?php

// function to clean post data
function cleanPost(&$value) {

    if (is_array($value)) {
        foreach ($value as $k => $v) {
            $value[$k] = cleanPost($v);             
        }
        return $value;
    }
    else {
        $value = mysql_real_escape_string($value);
        return trim(htmlentities(strip_tags($value)));
    }
}

// search function
function search() {

    // check if post data is set
    if (isset($_POST['searchBar'])) {

        // make link with db
        $link = mysqli_connect('localhost', 'scott', 'tiger','courses');
        if (!$link)         
            return false;
        }

        // clean your post data
        $cleanPostData = cleanPost($_POST);

        // query
        $sql = "SELECT title, summary, id FROM course WHERE title LIKE '%".$cleanPostData['searchBar']."%'";
        $result = mysqli_query($link, $sql);

        // iterate over results
        if (isset($result) && mysql_num_rows($result) > 0) {
            while ($row = mysql_fetch_assoc($result)) {
                // here is your data
                echo $row['title'] . "< br/>";
                echo $row['summary'] . "< br/>";
                echo $row['id'] . "< br/>";
            }
        }       
    }
}

// call search function
search();

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM