Problems in returning data when creating a search in php

1

I'm creating a search.php for my system, so far everything is OK, however in the while that I created to show future questions that hit with the searched title and their respective links, nothing appears on the page.

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

I'd like to see a better view in SQL to see if there are any errors below in the search or how to display with 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 -->
    
asked by anonymous 04.09.2015 / 14:33

1 answer

2

When assembling a database, we need mechanisms to extract the data from that database, and the way we do this is through SQL queries. A SQL query is nothing more than a query we ask the database. For the answer to be happy it is necessary to ask the question well and to help us at the time of making the query is that we use SQL operators.

  

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

HOWEVER, "WHAT DOES IT SERVE" OR THE OPERATOR LIKE?

The LIKE operator is used in a WHERE clause to search for a default string, using LIKE with Underline (_) and Percentage (%).

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

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

Inthequeryabove,weaskedthebankthroughSQL,whicharetherecordswherethesecondletterisU.TheSublinhado(_)meansthepositionofthefirstletter,thatis,wedonotneedtoknowwhichletteristhefirstletter.Youcanchangethepositionaccordingtoyourneed.

SelectingallrecordsthatstartwiththeletterJ:

Select*fromPessoaWhereNomeLIKE'J%';

Font

    
04.09.2015 / 15:23