需要帮助使用PHP和MYSQL实现简单的东西

时间:2011-02-23 20:38:31

标签: php mysql database

这是我的代码 -

<?php 

$u = $_SESSION['username'];
while($fetchy = mysqli_fetch_array($allusers))
{

mysqli_select_db($connect,"button");
$select = "select * from button where sessionusername='$u' AND response = 'approve'";
$query = mysqli_query($connect,$select) or die('Oops, Could not connect');
$result= mysqli_fetch_array($query);
$email = mysqli_real_escape_string($connect,trim($result['onuser']));
echo $email; 

if($email){
    mysqli_select_db($connect,"users");
    $select_name = "select name, icon from profile where email = '$email'";
$query_2 = mysqli_query($connect,$select_name) or die('Oops, Could not connect. Sorry.');
$results= mysqli_fetch_array($query_2);
$name = mysqli_real_escape_string($connect,trim($results['name']));
$icon = mysqli_real_escape_string($connect,trim($results['icon']));
echo $name;
}

}

否则,db中有两个响应。因此,有两个名字得到回应,但它们都是相同的。为什么这样?例如

DB - NAMEs - Apple和Orange。

显示 - Apple Apple。

数据库示例 -

SESSIONUSERNAME      OnUSer

s@s.com              apple
s@s.com              orange

EDITED

使用@ endophage的方法 -

AppleOrange和AppleOrange。

2 个答案:

答案 0 :(得分:2)

由于您的循环现在已经存在,$u将始终相同,因此$select将始终具有相同的值,$email也是如此,$select_name也是如此因此,同样的记录不断回归也就不足为奇了。

修改

如果$select_name查询返回多个结果,那么您需要像其他查询一样使用while循环遍历结果。

答案 1 :(得分:0)

试试这个,你的while循环在错误的地方:

<?php 

$u = $_SESSION['username'];


mysqli_select_db($connect,"button");
$select = "select * from button where sessionusername='$u' AND response = 'approve'";
$query = mysqli_query($connect,$select) or die('Oops, Could not connect');
while($result = mysqli_fetch_array($query))
{
  $email = mysqli_real_escape_string($connect,trim($result['onuser']));
  echo $email; 

  if($email){
      mysqli_select_db($connect,"users");
      $select_name = "select name, icon from profile where email = '$email'";
      $query_2 = mysqli_query($connect,$select_name) or die('Oops, Could not connect. Sorry.');
      $results= mysqli_fetch_array($query_2);
      $name = mysqli_real_escape_string($connect,trim($results['name']));
      $icon = mysqli_real_escape_string($connect,trim($results['icon']));
      echo $name;
  }
}