Get name through user ID

Asked

Viewed 2,341 times

1

I have to take my user id and save in a variable the name related to this id, but it is not working !

    function inReg() {

    global $connect;

    $id_usuario = $_POST["id_usuario"];
    $denuncia = $_POST["denuncia"];

    $nome = "SELECT nome FROM usuario WHERE id_usuario = '$id_usuario'";

    $query = " Insert into denuncia (id_usuario, denuncia) values 
   ('$id_usuario','$denuncia')";
    mysqli_query($connect, $query) or die (mysqli_error($connect));
    mysqli_close($connect);



    require 'rotinas_emails/registro_denuncia_mail.php';

 }

 ?>

In this variable $name is saving the sql itself " New Report sent by SELECT name FROM user WHERE id_usuario = '74'"

  • 2

    You need to execute select in your code vc only created a string with an sql command.

  • the id_usuario of the two tables are equal? for which the select name of the user table?

  • Actually and only one table, this select I do to send the variable name to another page that will be sent an email with the data.

2 answers

4

You have to run the query and read the returned value, to do this, this way:

$select = "SELECT nome FROM usuario WHERE id_usuario = '$id_usuario'";
$resultado = mysqli_query($connect, $select);
row = mysqli_fetch_row($resultado);
$nome = row[0];
  • Okay, I had tried in similar ways, but when I put that code in my bank it doesn’t even save, as if it stops running on these lines .

  • From what you’re saying, it seems to be the case mark an answer as accepted. If you have an answer that really helped you, mark it as accepted. So content is more organized and easier to find in the future by other people with similar problems.

  • 2

    hahaha, many come here take the solution and disappear without at least mark as accepted, one up to make up for the work

2

Remember, a query does not auto-run and takes the database information. To do this you need to use the mysqli_fetch_array.

mysqli_fetch_array will return a row corresponding to your query to the database.

Then you create your query, run it in mysqli_query, and take the line with fetch_array. After that you will have how to access the index of this result. For example.

$nome = mysqli_fetch_array($resultado);

echo $nome['a_coluna_que_deseja_saber_do_banco']; 

After you’ve done all this, just play the result in a query and there.

I’m not very familiar with mysqli... but that’s basically it.

  • I didn’t understand the part about playing in a query

  • For example this line I posted Oce took a number of denunciations.. $nome['qtdDenuncias'] . Ai based on this You need to create another query ... $queryProcura = "SELECT * FROM table WHERE id = '1' AND qtdDenunciasUser = {'$name[qtdDenunces]'}; .... Just an example ,does not make sense but an example that Voce can take the result of a database line of a query and put in another query for another query. But if I made it complicated, I tried to make it easy.

Browser other questions tagged

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