Pick name through user ID

1

I have to get the id of my user and save in a variable the name of 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 userid = '74'"

    
asked by anonymous 16.06.2017 / 20:36

2 answers

5

You have to execute the query and read the value returned, to do this, like this:

$select = "SELECT nome FROM usuario WHERE id_usuario = '$id_usuario'";
$resultado = mysqli_query($connect, $select);
row = mysqli_fetch_row($resultado);
$nome = row[0];
    
16.06.2017 / 20:40
2

Remember, a query does not auto-execute and takes the information from the database. For this you need to use mysqli_fetch_array.

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

Then you create your query, execute it in mysqli_query, and take the line with fetch_array. After that you will have access to 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.

    
16.06.2017 / 20:52