Query problem using SQL in PHP

Asked

Viewed 42 times

0

I have the following code:

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

In it, the variable methodName receives the value of a form and then I printed it. Until then it works correctly.

Turns out I need to use this variable for comparison and make a SELECT on 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;
}

Turns out he doesn’t return anything to me on echo $id. On the other hand, you have the right information in the bank to return.

Where am I wrong? I don’t see anything wrong in the query.

  • Duplicate: https://answall.com/q/326721/99718

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 didn’t quite understand your problem, but seeing it solves! it returns the id according to your WHERE $nameConteud..

0

Paul. You probably have a problem with your consultation.

Assuming the user typed "Church" in the form and made a Ubmit you will have the following code within 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 it was made a $_POST and this value is different from empty you will perform the query. Then let the 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 the user typed in the form is the same as the database I recommend using the operator LIKE SQL. Otherwise you can use a simple =, but you’ll have to make sure the words are the same.

Going back to the form page script let’s call the method. For this, I will use a class called Content, where the method is requestID.

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

Having the query 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 perform query (unless necessary). The ideal, if possible, would be to work with the ID, which would bring more performance to the query and a greater guarantee of "success" in it.

See more:

LIKE operator

Practical guide to mysqli in PHP

Browser other questions tagged

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