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?

1
  • 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 Commented Jul 1, 2014 at 20:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

While fetching the records use a while loop :

 <?php
  $result = mysql_query($sql);
  while($row = mysql_fetch_assoc($result)){
   echo $row['name'];
  }
 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.