首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP和mysql查询以检索名称

PHP和mysql查询以检索名称
EN

Stack Overflow用户
提问于 2012-10-23 21:58:03
回答 2查看 109关注 0票数 1

我有以下代码,它根据用户的登录详细信息从表中获取用户数据

代码语言:javascript
复制
//==========================================
//  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循环中,但随后它会根据用户的记录多次打印出来。

你能帮上忙吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-23 22:04:21

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

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

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

代码语言:javascript
复制
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.
}
票数 1
EN

Stack Overflow用户

发布于 2012-10-23 22:04:34

你的逻辑是错误的。您正在制作学生表,并且您想要回显欢迎使用logged_in的学生

但是你使用的是echo "<p>Welcome "; echo $row[studentName];

未设置$row的位置...此名称应来自以前的select ..

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13032169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档