简体   繁体   English

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

[英]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 = 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;
}

} }

NOw, there are two reponses in db. 不,db中有两个响应。 So, two names are getting echoed, but they both are SAME. 因此,有两个名字得到回应,但它们都是相同的。 Why so? 为什么这样? Eg 例如

DB - NAMEs - Apple and Orange. DB - NAMEs - Apple和Orange。

Displayed - Apple Apple. 显示 - Apple Apple。

Database example - 数据库示例 -

SESSIONUSERNAME      OnUSer

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

EDITED EDITED

Using @endophage's method - 使用@ endophage的方法 -

AppleOrange and AppleOrange. AppleOrange和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. 正如你的循环现在一样, $u将永远是相同的,因此$select将始终具有相同的值,因此$email也是如此,因此$select_name也是如此,因此同样的记录不断回归也就不足为奇了。

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. 如果$select_name查询返回多个结果,那么您需要像其他查询一样使用while循环遍历结果。

Try this, you had your while loop in the wrong place: 试试这个,你的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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM