0
<?php 
 $droll=$_SESSION['roll'];
 $sql=" SELECT name FROM hostel_register WHERE roll=$droll";
 $result=mysql_query($sql);
 echo $result;
?>

This code instead of returning name returns value "Resource id #5" everytime. Can someone help?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Piyush Singh
  • 533
  • 5
  • 17
  • 1
    This behavior is expected and defined. The successful execution of the `mysql_query` function returns a **resultset** object, not a scalar value, not an array, etc. (N.B. **Warning** This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.) [http://www.php.net/manual/en/function.mysql-query.php](http://www.php.net/manual/en/function.mysql-query.php) – spencer7593 Jul 01 '14 at 20:40

2 Answers2

3

You forgot to fetch your results

$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo $row['name'];

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

While fetching the records use a while loop :

 <?php
  $result = mysql_query($sql);
  while($row = mysql_fetch_assoc($result)){
   echo $row['name'];
  }
 ?>
Jignesh Rawal
  • 521
  • 6
  • 17