Problem with query using SQL in PHP

0

I have the following code:

$nomeConteudo = $_POST['tipoConteudo'];
echo $nomeConteudo;
$id = $conteudo_fkid->cataId($nomeConteudo);
echo $id;

In it, the variable nameName receives the value of a form and then prints it. Until then it works correctly.

I happen to need to use this variable for comparison and make a SELECT in my table conteudo , and return the ID value.

This is the method code:

function cataId($nomeConteudo){
  $query = "select id from conteudo where nomeConteudo = {$nomeConteudo}";
  $resource = mysqli_query($this->conexao, $query);
  return $resource;
}

It turns out that it does not return anything to me in echo $id . On the other hand, you have the right information in the bank to be able to return.

Where am I going wrong? I can not see anything wrong in the query.

    
asked by anonymous 02.09.2018 / 00:42

2 answers

0
$query = "SELECT c.id id_conteudo FROM conteudo c WHERE nome = '$nomeConteudo'";

//Buscar
  $resource = mysqli_query($conexao, $query);

//Resultado
  if ($resource == null) {
  echo"Nenhum registro encontrado!";
  }else {
        while ($row = mysqli_fetch_assoc($resource)) {
        echo "ID: " . $row['c.id'];
        }
      }

I do not understand your problem well, but see that solves it! it returns the id according to its WHERE $ filename.

    
02.09.2018 / 05:53
0

Paul. You're probably having trouble with your query.

Assuming that the user typed "Church" in the form and made a submit you will have the following code inside the page itself (or another) to receive the data and perform the query by type;

if (isseet($_POST['tipoConteudo']) && $_POST['tipoConteudo'] != "") {
   $nomeConteudo = $_POST['tipoConteudo'];
   echo $nomeConteudo;
}

If a $_POST has been made and this value is different from empty you will perform the query. So let's function:

function requestID($nomeConteudo){
    $query = "SELECT id FROM conteudo WHERE nomeConteudo LIKE '%$nomeConteudo%'";
    $id = mysqli_query($this->connection, $query);

    return $id;
}

If you are not sure that the word that the user typed in the form is the same as the database I recommend using the LIKE SQL operator. Otherwise you can use a simple = , but you have to make sure the words are the same.

Returning to the page script of the form we will call the method. For this, I'm going to use a class called Content, where the requestID method is found.

if (isseet($_POST['tipoConteudo']) && $_POST['tipoConteudo'] != "") {
   $nomeConteudo = $_POST['tipoConteudo'];
   echo $nomeConteudo;
   $Conteudo = new Conteudo();
   $id_consulta = $Conteudo->requestID($nomeConteudo);
   echo $id_consulta;
}

Having the consultation method ready the rest was simple. It was enough to instantiate the class where the method was and pass the variable received by $_POST as a parameter.

Tip:

Do not work with Strings to query (unless necessary). Ideally, if you can, it would work with the ID, which would bring more performance to the query and a greater guarantee of "success" in it.

See more:

Operator LIKE

Practical guide to mysqli in PHP

    
03.09.2018 / 01:33