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

这是我的代码,我想显示当前登录用户的卷号,当我运行此代码时,我没有选择任何数据库。

4

6 回答 6

1

首先,您应该正确编码,这意味着数据库连接和数据库选择应该放在首位:

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

也移到mysql_close();了最后
另一个要点是,现在mysql_已弃用,请使用mysqli_

于 2013-09-13T09:57:19.373 回答
1

从第 10 行删除此语句

 mysql_close();
于 2013-09-13T09:54:56.940 回答
0

使用此代码并在最后使用 mysql_close 函数。

<?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();
   ?>
于 2013-09-13T10:13:02.277 回答
0

mysql_query mysql_close(); 在尝试此操作之前,您已关闭来自 MySQL 的连接

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

请不要mysql_*在新代码中使用函数。它们不再被维护并被正式弃用。看到红框了吗?改为了解准备好的语句,并使用PDOMySQLi -本文将帮助您决定使用哪个。如果您选择 PDO,这里有一个很好的教程

于 2013-09-13T09:55:28.090 回答
0

只需在之前删除这两行即可解决您的问题

 $result = mysql_query($query) or die(mysql_error());
  $rows = array();
于 2013-09-13T09:57:14.133 回答
-1

顺序应该是这样的

 mysql_select_db('meipolytechnic', $link); 


$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
    mysql_select_db('meipolytechnic', $link);
于 2013-09-13T09:55:53.743 回答