2012-10-23 88 views
1

我有以下的代码,从表中根据自己的日志中的细节得到了用户的数据PHP和MySQL查询,以获取名称

//========================================== 
// CONNECT TO THE LOCAL DATABASE 
//========================================== 
$user_name = "xxxx"; 
$pass_word = "xxxxx"; 
$database = "xxxx"; 
$server = "xxxxxx"; 

$db_handle = mysql_connect($server, $user_name, $pass_word); 
$db_found = mysql_select_db($database, $db_handle); 

if ($db_found) { 
    $SQL = "SELECT * FROM students WHERE L1 = '$uname' AND L2 = '" .md5 ($_POST['password'])."'"; 
    $result = mysql_query($SQL); 
    $num_rows = mysql_num_rows($result); 

    //==================================================== 
    // CHECK TO SEE IF THE $result VARIABLE IS TRUE 
    //==================================================== 

    if ($result) { 
     if ($num_rows > 0) { 
      $color="1"; 

      $result = mysql_query("SELECT * FROM entry, students WHERE entry.studentName = students.studentName AND students.L1='$uname' ") or die(mysql_error()); 

      echo "<p>Welcome "; echo $row[studentName]; 
      echo "<p>You records as of "; 
      echo date('l jS \of F Y h:i:s A'); 

      echo "<table border='1' cellpadding='2' cellspacing='0'>"; 
      echo "<tr> <th>Date</th><th>Student Name</th> <th>Tutor name</th> <th>Procedure name</th> <th>Grade</th><th>Student Reflection</th><th>Tutor Comments</th><th>Professionalism</th> <th>Communication</th> <th>Alert</th> <th>Dispute</th> </tr>"; 
      // keeps getting the next row until there are no more to get 

      while($row = mysql_fetch_array($result)) { 
       if($color==1){ 
        echo "<tr bgcolor=#DDD ><td>".$row['date']."</td><td>".$row['studentName']."</td><td>".$row['tutorName']."</td><td>".$row['procedureName']."</td><td>".$row['grade']."</td><td>".$row['studentReflection']."</td><td>".$row['tutorComments']."</td><td>".$row['professionalism']."</td><td>".$row['communication']."</td><td>".$row['alert']."</td><td>".$row['dispute']."</td></tr>"; 

        // Set $color==2, for switching to other color 
        $color="2"; 
       } 
       // When $color not equal 1, use this table row color 
       else { 
        echo "<tr bgcolor='#CCC'><td>".$row['date']."</td><td>".$row['studentName']."</td><td>".$row['tutorName']."</td><td>".$row['procedureName']."</td><td>".$row['grade']."</td><td>".$row['studentReflection']."</td><td>".$row['tutorComments']."</td><td>".$row['professionalism']."</td><td>".$row['communication']."</td><td>".$row['alert']."</td><td>".$row['dispute']."</td></tr>"; 
        // Set $color back to 1 
        $color="1"; 
       } 
      } 
      echo '</table>'; 
     } 

我想这样做是回声或打印出用户名在桌子上方,但我很难这样做。唯一需要的就是将它添加到while循环中,然后将其打印出来的次数与用户记录的次数相同。

你能帮忙吗?

+0

我看到一个错误代码:你声明$ user_name和在您的SQL查询你使用$ UNAME –

+0

这是没有问题的。我可以很好地连接到数据库并获取用户记录,但在生成表之前,我想回显用户全名,该用户全名保存在学生表中。$ uname用于我使用的html日志类型表单让学生登录 – user1132612

+0

不是说这会回答你的问题,但如果你使用PHP5,你不应该使用mysql_命令,因为它们已被弃用。您应该切换到使用mysqli或PDO。 – Codeguy007

回答

1

您将需要获取第一行以访问用户信息。这意味着你必须在循环之前调用一次mysql_fetch_array

这会给你带来麻烦,因为你还需要在循环中调用它。你可以通过使用各种布尔标志或行的副本来解决这个问题,但最好的方法是稍微改变代码的结构。

使用do-while loop,与if statement结合。这允许您首先获取单个行,如果没有找到,则采取特殊的操作。之后,你得到一个循环,它现在执行它的操作,只有它检查之后是否有下一行而不是之前的迭代,否则第一行将在表输出中跳过。

if ($row = mysql_fetch_array($result)) { 
    // Print user info 
    // Print table header 

    do { 

    // Print table row 

    } while ($row = mysql_fetch_array($result)); 

    // Print table footer 
} 
else 
{ 
    // User not found. Print error or whatever. 
} 
0

你有错在你的逻辑...... 您正在秋源表,并且要呼应欢迎它LOGGED_IN

的学生,但您使用echo "<p>Welcome "; echo $row[studentName];

凡$行没有设置...这个名字应该来自一个以前的选择..