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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?