Error in select with PHP + Mysqli

Asked

Viewed 644 times

2

I’m trying to use a query to pull one ID specific that comes from another page.

I can receive the ID for $_GET, i printed the query to see if it was right, even so of the error and does not show me which error 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 by taking this query and running right through the phpMyAdmin and it worked, I don’t need in this case an array of querys is just a specific tuple, but the error and $mysqli->error don’t tell me what the mistake is.

  • $codigo has something like codigo = 1 remember to specify which field you want to compare. You put an example of what the query output is like

2 answers

3


Your query is wrong because you are not comparing $code with any value. Notice:

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

id_puppies is the primary key of the puppies table.. I invented this name to make you realize.. but I don’t know what the primary key of your table is... I don’t know if it’s with this value that you want to compare, but it 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..

You see, Voce says: Select All Data from Table Puppies Where Code....... and then say no more.... IE, Voce has to say Select all data from the table puppies where the "table field" is equal to the code........

Good luck.

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;
}
?>

Browser other questions tagged

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