开发者

Need Help With Implementing Simple Stuff with PHP and MYSQL

Here is my code -

<?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 = m开发者_如何转开发ysqli_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;
}

}

NOw, there are two reponses in db. So, two names are getting echoed, but they both are SAME. Why so? Eg

DB - NAMEs - Apple and Orange.

Displayed - Apple Apple.

Database example -

SESSIONUSERNAME      OnUSer

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

EDITED

Using @endophage's method -

AppleOrange and AppleOrange.


As your loop stands now, $u will always be the same, so $select will always have the same value, and so will $email, and so will $select_name, so it is no surprise that the same record keeps coming back.

Edit

If the $select_name query returns multiple results, then you need to loop through the results with a while loop like the other queries.


Try this, you had your while loop in the wrong place:

<?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;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜