Why does my PHP script only return me Resource id # 4? [closed]

1

I'm trying to get only the name of a column from a database table, and it always returns only the Resource id # 4, I'm going to send a portion of my code.

<?php
    mysql_connect('localhost', 'root', '') or die ("Erro ao conectar!");
    mysql_select_db('agenda') or die ("Erro ao conectar!");
?>

<?php
   $iid = $_GET['id'];
   //echo $iid;
   $nomepessoa = "SELECT nome FROM pessoa WHERE id = $iid";

   $queryy = mysql_query($nomepessoa);

   echo $queryy;

?>
    
asked by anonymous 08.09.2016 / 20:03

1 answer

4

mysql_query() returns a resource or a false in case of error. To extract the information it is necessary to use mysql_fetch_assoc() or another return flavor. Three important things,

  • The mysql_ * functions have already been removed from php7 so it is better to use the PDO or MySQLi.

  • mysql_fetch_assoc() or mysql_fetch_array() return only one line, if you want to return another, a while is required.

  • Give your variables meaningful names.

Your corrected code should look like this:

$sql = "SELECT nome FROM pessoa WHERE id =" .mysql_real_escape_string($iid);
$query = mysql_query($sql);
$pessoa = mysql_fetch_assoc($query);
echo $pessoa['nome'];
    
08.09.2016 / 20:10