-4

So I'm having this issue with showing the output of a query. I will show my code and try to explain the best I can.

<?php
    include_once('ligacao.php');
    if (mysqli_connect_errno())
    {
    echo "Falha ao conectar à Base de Dados: ".mysqli_connect_error();
    }

        $pesq = $_POST['id'];
        echo $pesq; 

        $sqlveri = "SELECT * FROM distrito WHERE iddistrito LIKE '12'";
        $result = mysql_query ($ligar,$sqlveri);
        echo $resul;
        mysqli_close ($ligar);
        ?>

The only thing that appears when I submit is the number that is kept on the variable $pesq. Help me out here guys I have been grinding for an answer for 2 straight days with no luck at all

Thank you for your help.

picaluga
  • 95
  • 8
  • 1
    so you changed `mysql_fetch_array` to `mysqli_fetch_array`. How do we know you're not using `mysql_` to connect with or PDO? Plus, you're not checking for errors. Also, no idea what variable you are using to connect with. – Funk Forty Niner Dec 17 '15 at 13:02
  • I reopened your question and I have posted my answer for you below. Edit: you deleted your comment that I replied to here. – Funk Forty Niner Dec 17 '15 at 13:18
  • you just ***completely changed*** your question from your original post and http://stackoverflow.com/revisions/34335181/2 - I won't be offering any further help on this, sorry. You need to read more tutorials and manuals. Had I known this ahead of time, I'd of never submitted an answer. – Funk Forty Niner Dec 17 '15 at 13:35
  • and this is wrong `$result = mysql_query ($ligar,$sqlveri); echo $resul;` I have done what I could here. Good luck with your project. – Funk Forty Niner Dec 17 '15 at 13:38

1 Answers1

5

Note to future visitors.

The original closure of their question was based on their original post where they were using mysql_fetch_array without the i, and changed it before it could be recorded in revisions.

Then completely changed their code https://stackoverflow.com/revisions/34335181/3 after I posted my answer.


Original answer based on the above

So you changed mysql_fetch_array to mysqli_fetch_array after I closed the question about your mixing MySQL APIs.

It is unclear as to which MySQL API you're using to connect with. mysql_, mysqli_, or PDO (those different APIs do not intermix).

Plus, you're not checking for errors.

Also, no idea what variable you are using to connect with.

What's going on here is that you're not connecting to your query and I do not know what variable you are using.

So, base yourself on the following and change the variable to the one you're using in your connection file:

$result = mysqli_query($connection, "SELECT ...

mysqli_query() requires a connection:

Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything, as well as or die(mysqli_error($connection)) to mysqli_query().

Your HTML form is also unknown, and if it is using a POST method and if the input does have the name attribute for it.

I.e.:

<form method="post" action="your_handler.php">
  <input type="text" name="id">
</form>
  • Forms default to a GET if a POST method isn't specifically set. Therefore, that would trigger/cause an undefined index notice.

Sidenote: Make sure there are no whitespaces in your input/query. Use trim() and var_dump() as an additional tool.

Plus, if you're looking for an exact match, don't use LIKE, but a WHERE iddistrito = '$pesq'");

Read up on LIKE: http://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html

There are wildcards you can use, if pattern matching is required.

Your present code is open to SQL injection. Use mysqli_* with prepared statements, or PDO with prepared statements.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Great explanation Fred you've touched every point that is wrong with his code. I have two little question: If I enable `error_reporting(E_ALL);` how will it display without me specifying it at a specific moment in the code like `mysqli_error($connection)` for example? second do I still have to use `mysqli_error($connection)` if I enable `error_reporting(E_ALL);`? – BRoebie Dec 17 '15 at 13:27
  • @BRoebie Thanks. only using `error_reporting(E_ALL);` will not display errors, but log them, therefore using `ini_set('display_errors', 1);` is best to display them if people don't have access to logs. – Funk Forty Niner Dec 17 '15 at 13:29
  • thank you for your fast reply so if I am understanding it correctly if I use this ` error_reporting(E_ALL); ` combined with this `ini_set('display_errors', 1);` I don't have to use any other error checking in for example the connection string or the query with `mysqli_error()`? – BRoebie Dec 17 '15 at 13:33
  • @BRoebie you're welcome. It's best to check for errors for both PHP and MySQL while testing. Then, to remove them for production once everything works. – Funk Forty Niner Dec 17 '15 at 13:35
  • Okay I understood all of the points you refered. I have the connections there, I changed it for the sake of "try everything". The mysqli's go for the same. You are very correct about the LIKE in the mysql statement. I have already changed it. – picaluga Dec 17 '15 at 13:38
  • @Fred-ii- Thanks for the detailed explanation sorry if I sounded like a bit of a stupid idiot. But I wanted to achieve that if somebody came by who just started learning PHP he/she would understand why you would need that to do :D – BRoebie Dec 17 '15 at 14:38
  • @BRoebie You're welcome. I can only help with what I see before me. You're not a *"stupid idiot"*, you're learning and that doesn't make you "stupid". I tried helping the best I could with what I had to work with. If there's something that's unclear/unknown somewhere, then I can't help there since I don't know what I'm up against ;-) I wish you all the very best, *cheers* – Funk Forty Niner Dec 17 '15 at 14:41