Data return problems when creating a php search

Asked

Viewed 43 times

1

I’m creating a search.php for my sisteminha, so far everything is OK, but no while that I created to show the questions that match the searched title and their respective links, nothing appears on the page.

No SQL error or anything, just does not return any data.

I would like a better view on SQL to check if there are any errors below in the search or how to display with the while.

<?php

            require_once("php/conexao.php");

            if (isset($_GET['search'])) {

                $textoBusca = $_GET['search'];

                $strSQL = "SELECT * FROM questoes WHERE titulo LIKE '".$textoBusca."'";

                $result = mysqli_query($conexao, $strSQL); 

                ?>

                <div class="row">
                    <div class="col-lg-12 text-center">
                        <div class="page-header texto-pesquisa">
                            <h2>Você buscou por "<?php echo $textoBusca; ?>"</h2>
                        </div>
                     </div>
                </div>
                <!-- /.row -->

                <div class="row">
                    <div class="col-lg-12 text-center">
                    <?php

                        while($row = mysqli_fetch_assoc($result)) {
                            $artigos = $row['titulo'];


                    ?>

                            <p><?php echo $artigos; ?></p>


                    <?php } ?> <!-- fecha o while -->
                     </div>
                </div>
                <!-- /.row -->

        <?php } ?> <!-- Fecha o IF -->
  • The search in the bank returns some record?

  • 3

    Barter LIKE '".$textoBusca."' for LIKE '%".$textoBusca."%' and see what comes back.

  • Perfect, @henriquedpereira, it worked! I always have these problems with copying the SQL command to PHP and printing it right. Any tips? Rs

  • Take a look at the documentation to better understand the use of LIKE = https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like

  • Opa... for security, it would be better to change the way the query is mounted and use the mysqli prepare to avoid injections.

1 answer

2

When building a database, we need mechanisms to extract the data from that database, and the way to do that is through SQL queries. An SQL query is nothing more than a question we ask the database. For the answer to be satisfied it is necessary that the question is well asked and to help us when making the query is that we use the SQL operators.

In your case, change LIKE '".$textoBusca."' for LIKE '%".$textoBusca."%'

HOWEVER, "WHAT IT’S FOR" THE LIKE OPERATOR?

Use the LIKE operator in a WHERE clause to search for a standard string, using LIKE with Underscore (_) and Percentage (%).

  • Sublinhado (_) - Serves to mark a specific position

  • Porcentagem (%) - Any character from the specified position.

inserir a descrição da imagem aqui

In the above query, we ask the database through SQL, which are the records where the second letter is U. The Sublinhado (_) means the position of the first letter, ie we do not need to know which is first letter. You can change the position according to your need.

Selecting all records that start with the letter J:

Select * from Pessoa Where Nome LIKE 'J%';

inserir a descrição da imagem aqui

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.