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.

0

6 Answers 6

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_

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

Comments

1

Remove this statement from line number 10

 mysql_close();

Comments

0

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

 $result = mysql_query($query) or die(mysql_error());
  $rows = array();

Comments

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();
   ?>

Comments

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.

Comments

-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);

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.