How to know if the database query was successfully performed?

Asked

Viewed 804 times

1

I have a database of people, the name of my db and Register and in it I have a table users with four columns already filled id, age, name and email usuarios cadastrados 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 as I think my error is in the $consult I am completely lay in Mysql

print of the result in the browser print do resultado no navegador

  • remembering I am wanting to retrieve the column name of my db referring to my GET

  • 1

    Print 'found nothing'? What prints?

  • the GET parameter is sent but it is not printing me anything

  • Sent as? Do not print anything on the page? Show how you are doing, url where you are trying to remove the parameter

  • I put a print on the question see @Miguel

  • What do you print? Print nothing?

  • Then right wouldn’t post not? or there’s nothing to post and get?

  • In this case it’s fine, it’s $_GET['q']... But don’t print anything? Do it there echo $_GET_['q'];

  • GET parameter is printing but query is not responding

  • Try the connection parameter first($connect,"SELECT .....)

  • and maybe, $Consult="SELECT...." ; $result=mysqli_query($connect,$Consult); and ai $Row=mysqli_fetch_assoc($result); print_r($Row);

  • I tried too now and nothing

  • Try that last landing, I sent, in the comments..

  • now returned was not found anything @Magichat

  • 1

    @Nikobellic Hey bro, Funfou?

  • funfo vlw @Magichat

  • I edited the question because "phpmyadmin" is not a "database", read this to understand the differences: What is the difference between mysql and phpmyadmin?

Show 12 more comments

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>

  • I have tested nothing and nothing

  • see so beautiful...

  • ops changes $query to $name...

  • guy now finally returned me an Array ( [name] => July )

  • Is there something missing or was it just that?

  • that’s right but I wanted to know where I went wrong +1 was worth

Show 1 more comment

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

  • did not return anything @Andre Balil

  • Does a message appear? Or does the array() reset?

2

Though I don’t recommend that way, you should use Prepared statments, but the error is that you are not actually using the variable that contains the value of $_GET['q']. You have to insert into the query what you are looking for does so:

<?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';
        }
    ?>
  • now returned nothing found @Miguel

  • to achieve it was already mistake of achievement

  • Yes, but yours is if(isset(... as it is, it is subject to error. you can test by doing www.teusite.com?j=nome, instead of ?q=... You must make the condition for the $_GET['q'] and not to the $nome

  • True guy this subject to error even @Miguel

  • how I do to solve this?

  • Did you make it? @Nikobellic

  • yes I added an if($name != NULL)

  • Okay, that’s @Nikobellic, it’s the same thing

Show 3 more comments

Browser other questions tagged

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