0
    <?php
    $username = "root";
    $password = "password";
    $database = "xxxxxx";
    $link = mysql_connect("localhost", $username, $password);    
     $query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
    mysql_select_db('xxxxxx', $link);
     $result = mysql_query($query) or die(mysql_error($link));
     $num = mysql_num_rows($result);
     mysql_close();

  $rows = array();

  $result = mysql_query($query) or die(mysql_error());
  $rows = array();
  while($r = mysql_fetch_row($result))
  {
   $rows[] = $r[0];
   }
   print_r($rows); 
   ?>

This is my code i want to display the roll number of the currently logged in user and when i run this code i get no database selected.

user2774977
  • 89
  • 2
  • 3
  • 11

6 Answers6

1

Remove this statement from line number 10

 mysql_close();
Kalpit
  • 4,906
  • 4
  • 25
  • 43
1

First of all, you should code properly, means database connection and database selection should be on top:

<?php
    $username = "root";
    $password = "password";
    $database = "xxxx";
    $link = mysql_connect("localhost", $username, $password);
    mysql_select_db('xxxx', $link);

    $query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
    $result = mysql_query($query) or die(mysql_error($link));
    $num = mysql_num_rows($result);

    $rows = array();
    $result = mysql_query($query) or die(mysql_error());
    $rows = array();
    while($r = mysql_fetch_row($result))
    {
        $rows[] = $r[0];
    }

    print_r($rows);
    mysql_close();
   ?>

Also moved mysql_close(); on last
One other main point was, now mysql_ is deprecated, please use mysqli_

user2774977
  • 89
  • 2
  • 3
  • 11
Shreyas
  • 247
  • 1
  • 14
0

You have closed the connection from MySQL before the mysql_query mysql_close(); try this

    <?php
    $username = "root";
    $password = "password";
    $database = "dfsdftwsdgdfgdfsgsdf";
    $link = mysql_connect("localhost", $username, $password);    
     $query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
    mysql_select_db('xxxxxxx', $link);
     $result = mysql_query($query) or die(mysql_error($link));
     $num = mysql_num_rows($result);


  $rows = array();

  $result = mysql_query($query) or die(mysql_error());
  $rows = array();
  while($r = mysql_fetch_row($result))
  {
   $rows[] = $r[0];
   }
  mysql_close();
   print_r($rows); 
   ?>

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
0

just remove these two lines before while that will solve ur problem

 $result = mysql_query($query) or die(mysql_error());
  $rows = array();
Neophile
  • 1,519
  • 12
  • 15
0

Use this code and use mysql_close function in the last.

<?php
    $username = "root";
    $password = "password";
    $database = "xxxxxx";
    $link = mysql_connect("localhost", $username, $password);    
     $query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
    mysql_select_db($database, $link);
     $result = mysql_query($query) or die(mysql_error($link));
     $num = mysql_num_rows($result);

  $rows = array();

  $result = mysql_query($query) or die(mysql_error());
  $rows = array();
  while($r = mysql_fetch_row($result))
  {
   $rows[] = $r[0];
   }
   print_r($rows); 
   mysql_close();
   ?>
user2774977
  • 89
  • 2
  • 3
  • 11
-1

The order should be like this

 mysql_select_db('meipolytechnic', $link); 


$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
    mysql_select_db('meipolytechnic', $link);
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
Nithin Dev
  • 421
  • 10
  • 29