How do I know if the query in the database was successful?

1

I have a database with people's records, the name of my db and Registration and in it I have a table users > with four columns already filled id, age, name and email I am developing a user search system here is the search page

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="GET" >
            <input type="text" autocomplete="on" name="q" placeholder="Digite a sua pesquisa" />
            <input type="submit" value="Tecle Enter" />
        </form>
        <?php

            $host = 'localhost';
            $user = 'devel';
            $password = '********';
            $database = 'Cadastro';

            $connect = mysqli_connect($host, $user, $password, $database) or die("conexao die");
            $query = $_GET['q'];

            if(isset($query)){
                $consult = mysqli_query("SELECT nome FROM 'usuarios' WHERE 'nome' LIKE %q%", $connect);
                $result = mysqli_fetch_assoc( $consult );
                print_r($result);
            }

            else {
                echo 'nao foi encontrado nada';
            }
        ?>
    </body>
</html>

But I'm not getting feedback is there any way to know if the query was successful because I think my error is in the query of $consult I'm completely lay in MySQL

>

print the result in the browser

    
asked by anonymous 05.06.2016 / 17:12

3 answers

3

Making a mix of solutions ...

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="GET" >
            <input type="text" autocomplete="on" name="q" placeholder="Digite a sua pesquisa" />
            <input type="submit" value="Tecle Enter" />
        </form>
       <?php
        $nome = $_GET['q'];
        $host = 'localhost';
        $user = 'devel';
        $password = '********';
        $database = 'Cadastro';

       
        $connect = mysqli_connect($host, $user, $password, $database) or die("conexao die");
        mysqli_select_db($connect,'Cadastro');
        

        if(isset($nome)){
            $consult = 'SELECT nome FROM usuarios WHERE nome LIKE "%' .$nome. '%"';
            $result = mysqli_query($connect,$consult);
            $row = mysqli_fetch_assoc($result);
            print_r($row);
        }

        else {
            echo 'nao foi encontrado nada';
        }
        mysqli_close($connect); 
    ?>
    </body>
</html>
    
05.06.2016 / 17:32
2
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="GET" >
            <input type="text" autocomplete="on" name="q" placeholder="Digite a sua pesquisa" />
            <input type="submit" value="Tecle Enter" />
        </form>
        <?php

            $host = 'localhost';
            $user = 'devel';
            $password = '********';
            $database = 'Cadastro';

            $connect = mysqli_connect($host, $user, $password, $database) or die("conexao die");
            $query = $_GET['q'];

            if(isset($query)){
                $consult = mysqli_query("SELECT nome FROM 'usuarios' WHERE 'nome' LIKE '%".$query."%'", $connect);
                $result = mysqli_fetch_assoc( $consult );
                print_r($result);
            }

            else {
                echo 'nao foi encontrado nada';
            }
        ?>
    </body>
</html>

Note that the LIKE was incorrect, not passing the variable q correctly

    
05.06.2016 / 17:43
2

Although you do not recommend this way, you should use prepared statments , but the error is that you are not even using the variable that contains the $_GET['q'] value. You have to insert in the query what you are looking for:

<?php

        $host = 'localhost';
        $user = 'devel';
        $password = '********';
        $database = 'Cadastro';

        $connect = mysqli_connect($host, $user, $password, $database) or die("conexao die");

        if(isset($_GET['q'])){
            $consult = mysqli_query('SELECT nome FROM usuarios WHERE nome LIKE "%' .$_GET['q']. '%"', $connect);
            $result = mysqli_fetch_assoc( $consult );
            print_r($result);
        }

        else {
            echo 'nao foi encontrado nada';
        }
    ?>
    
05.06.2016 / 17:46