duanqian3464 2011-11-03 15:09
浏览 64
已采纳

请帮我调试这段代码

Bit confused with this one.

Basically, I have a column in a table, and I want to retrieve the value in that column, for a specific row.

I have one set of code that works, but not in this particular situation.

The code I have that works is this:

$qry="SELECT * FROM logins WHERE username='$login' AND password='$password'";
$result=mysql_query($qry);

This obviously retrieves that particular row, and then:

$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['User ID'];

This successfully sets the session variable, as the value from the user id column from this row.

So that's fine, that works. The code I am trying to use in my new situation is this:

$qry = "SELECT * FROM vote WHERE Username = '" .$_SESSION['SESS_USERNAME']. "'";
$result = mysql_query($qry);

This gets the row based on the current user, and then:

 while($row = mysql_fetch_array( $result )) 
 { 
 Print "<b>Name:</b> ".$row['Username'] . " <br>"; 
 Print "<b>Vote:</b> ".$row['Vote'] . " "; 
 } 

The while loop works correctly, it displays the current username, and their vote. Obviously I felt the loop was not required as I already have just the one row selected. Removing the loop broke it, so I put it back. Not a big deal, I can live with that if it gets the job done.

The issue I am focusing on, is the use of the

$row['Username']

In this if statement:

 if($row['Username'] == "Admin") {
 echo ("Win!<br />");
 }
 else {
 echo "Failed!";
 }

When printing from the loop above, it prints:

 Name, as Admin, 
and vote, as 0. 

If I then try to validate using the if statement, I ask it to echo Win! if the username == Admin.

Obviously it is true as it has printed it on the line before, however, it always prins Fail! and I can't understand why.

Should I set $row['Username'] as some other variable?

Can anyone offer their guidance please?

Cheers

Eds

  • 写回答

2条回答 默认 最新

  • douningzhi1991 2011-11-03 15:14
    关注

    The problem stems from your loop:

    while($row = mysql_fetch_array( $result )) 
     { 
     Print "<b>Name:</b> ".$row['Username'] . " <br>"; 
     Print "<b>Vote:</b> ".$row['Vote'] . " "; 
     } 
    

    The first time through, $row is set to the row of data from the database. It then tries to go through the loop again, but since there is only one row, mysql_fetch_array returns false. now $row is set to false. So any code after the loop won't have access to the data anymore.

    The solution would be to replace your loop with a simple if statement:

     if ($row = mysql_fetch_array( $result )) 
     { 
       Print "<b>Name:</b> ".$row['Username'] . " <br>"; 
       Print "<b>Vote:</b> ".$row['Vote'] . " "; 
     } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集