No select error with PHP + MySQLi

2

I'm trying to use a query to pull a specific ID from another page.

I can get ID through $_GET , I printed the query to see if it was right, even though it did not show me what error it was.

Code:

    <?php
    $mysqli = new mysqli('localhost', 'root', '', 'dog_house');
    if(!$mysqli)
        die(mysql_error());
    $codigo = $_GET['cod'];
    echo $codigo;//Ver a PK
    $sql = "SELECT * FROM 'filhotes' WHERE $codigo";
    echo $sql;//Ver a Query
    if ($mysqli->query($sql) === TRUE) {
    echo "Update no banco feito";
} else {
    echo "Erro:". $mysqli->error;
}
    ?>

I did the test taking this query and running right through phpMyAdmin and it worked, I do not need in that case an array of querys is just a specific tuple, but the error and $mysqli->error does not tell me what the error is.

    
asked by anonymous 31.05.2016 / 06:21

2 answers

3

Your query is wrong because you are not comparing $ code with no value. Repair:

 $sql = "SELECT * FROM filhotes WHERE id_filhotes = '$codigo';

id_files is the primary key of the table puppies..invented this name for you to realize .. but I do not know what the primary key of your table ... I do not know if it is with this value that you want to compare but is what makes sense. The important thing is that you realize that your query is wrong due to the fact that it does not compare anything with the Code ..

Note, you say: Select all data from the table puppies where the code ....... and then does not say anything else ... ie you have to say < in> Select all the data in the table puppies where the "table field" is equal to the code .... ....

Good luck.

    
31.05.2016 / 10:35
2
<?php

$mysqli = new mysqli('localhost', 'root', '', 'dog_house');
if(!$mysqli)
    die(mysql_error());
$codigo = $_GET['cod'];
echo $codigo;//Ver a PK

//aqui vc deve colocar o nome do campo com PK
//na sua tabela **filhotes** que vc quer comparar com
//o valor da variável $codigo
//veja minha alteração
$sql = "SELECT * FROM 'filhotes' WHERE codigo_filhote = '$codigo'";
echo $sql;//Ver a Query
if ($mysqli->query($sql) === TRUE) {
echo "Update no banco feito";
} else {
    echo "Erro:". $mysqli->error;
}
?>
    
31.05.2016 / 13:49