简体   繁体   English

如何搜索(在数据库行中)

[英]How can I search (In DB row)

I'm trying to find out how to search in a db row, but I have no IDEA... Maybe some of you guys can help out. 我正在尝试找出如何在数据库行中进行搜索,但我没有IDEA ...也许你们中的有些人可以帮忙。 I'm making a magic the gathering collection website for my boss. 我正在为老板制作一个收集收藏的网站的魔术。 In this game you have multiple sets and each set had a card. 在这个游戏中,您有多套,每套都有一张纸牌。 Theres a page in which you can see all sets, and in this page I want a search bar to find the sets. 这里有一个页面,您可以在其中看到所有集合,并且在此页面中,我希望搜索栏可以找到这些集合。 Want I want is a search bar at the top of the page and when you typ in "Strong" you get to see every set name which contains "Strong". 我想要的是页面顶部的搜索栏,当您键入“ Strong”时,您会看到包含“ Strong”的每个集合名称。

Output : http://prntscr.com/5ubsa2 List continues quite a bit... 输出: http : //prntscr.com/5ubsa2列表仍然有很多...

As you can see in the output, all the sets are listed. 正如您在输出中看到的那样,列出了所有集合。 I want that you can see the search results there. 我希望您可以在那里看到搜索结果。

Input : 输入:

<body>

<?php
    // Get data
    $query = 'select * from sets ORDER BY releaseDate;';
    $sets = $conn->query($query);

    // Generate HTML
    $htmlSets = '';
    foreach($sets as $row){
        $htmlSets .= '<div class="col-md-4 magic-set"><a href="'.$baseURL.'set.php?id='.$row['id'].'"><h5>'.$row['name'].'</h5></a></div>';
    }
    //set.php?fname=areg&lname=aerga

?>

<!-- Page Content -->
<div class="container">
    <?php include_once 'inc/navigation.php';?>
    <div class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="text-center">
                    <h3>Magic: the Gathering Sets</h3>
                    <hr>
                </div>
            </div>
        </div>

        <div class="row">
            <?= $htmlSets ?>
        </div>
        <!-- /.row -->
    </div>

    <?php include_once 'inc/footer.php';?>

</div>
<!-- /.container -->
</body>

It looks like you haven't started your MySQL connection, nor have you exited it. 看来您尚未启动MySQL连接,也没有退出它。

<?php

    define("DB_SERVER", "localhost");
    define("DB_USER", "username");
    define("DB_PASS", "password");
    define("DB_NAME", "database_name");

    $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

    // If the connection didn't succeed, run this error:

    if (mysqli_connect_errno()) {
        die("Database connection failed: " .
            mysqli_connect_error() .
            " (" . mysqli_connect_errno() . ")"
        );
    }
?>

This connection should be at the very start of your document. 此连接应该在文档的开头。 You also have to have mysqli_close($connection); 您还必须具有mysqli_close($ connection); at the end of your document in order to close your connection. 在文档末尾以关闭您的连接。

You also have a semicolon at the end of your query, so be sure to delete that. 您在查询末尾还会有一个分号,因此请务必将其删除。 This should be the correct formating of the query: 这应该是查询的正确格式:

$query = 'SELECT * FROM sets ORDER BY releaseDate';

You cannot use foreach loops for MySQL queries, you can only use while loops (unless you have auto-increment turned off for your primary id). 您不能对MySQL查询使用foreach循环,而只能使用while循环(除非您为主ID禁用了自动增量)。 Here is what your code will look like as a while loop: 这是您的代码在while循环中的样子:

<?php

    // You can also use mysqli_fetch_row()
    while ($row = mysqli_fetch_assoc($sets)) {
        $htmlSets .= '<div class="col-md-4 magic-set"><a href="'.$baseURL.'set.php?id='.$row['id'].'"><h5>'.$row['name'].'</h5></a></div>';
    }

    // Since your query is of a 'read' type and
    // not an 'update', you are required the free the result:

    mysqli_free_result($sets) ;

?>

Be sure to free your result, in case you need to use it again later. 确保释放您的结果,以防您以后需要再次使用它。

It is very simple bro actually you have to check that your saerch form is get or post you have always check if user search anything or it is empty for example it is get form and your query argument is "q" in php you will only need to do this 这是一个非常简单的兄弟,实际上您必须检查您的saerch形式是否为get或post,您始终要检查用户是否搜索了任何内容或该内容是否为空,例如,它是get形式,而您在php中的查询参数是“ q”去做这个

$query = "select * from sets ORDER BY releaseDate";
if( if(!empty($_GET['q'])))
{
    $query = "select * from sets WHERE name LIKE '%" .$_GET['q']. "%'    ORDER BY releaseDate";
}

this is just basic concept... 这只是基本概念...

You can do this with a bit of AJAX. 您可以使用一些AJAX来完成此操作。 Here's a sample. 这是一个样本。 Just replace with your values and CSS. 只需替换为您的值和CSS。

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

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