【问题标题】:How to echo all rows with certain aspect如何回显具有特定方面的所有行
【发布时间】:2018-06-23 19:13:58
【问题描述】:

我有两张桌子。它们通过一个(用户信息)到多个(成就)外键关系连接。我在下面尝试做的是回显所有具有给定 $usrid 的行。这可能不止一个。

不幸的是,它只回显其中一行的内容。如何更改它以回显存在某个用户 ID 的所有行?

<!DOCTYPE HTML>
<head>
<?php  $usrid = $_GET['usrid'];
$connection = @mysqli_connect("localhost","root","","Rain")
OR die('Could not connect' .
      mysqli_connect_error());

$query = "SELECT usrid, username, oldname, languages, joindate, art, hunting, frontwebdev, backwebdev, writing, programming, se, smm, pentesting, timezone, availability, reliability, profilePicture FROM userinfo WHERE usrid='" . $usrid . "';";
$response = @mysqli_query($connection,$query);
$row = @mysqli_fetch_array($response);
$username = $row['username'];

$achvquery = "SELECT achieveid, usrid, achievementname, achievementdescr, timestamp FROM achievements WHERE usrid=" . $usrid . ";";
$achvresponse = @mysqli_query($connection,$achvquery);
$achvrow = @mysqli_fetch_array($achvresponse);
$achvtitle = $achvrow['achievementname'];
$achvdescr = $achvrow['achievementdescr'];
?>
<title>
All Achievements
</title>
</head>
<body>
<span> <?php echo "<span> " . $username . "s OD Achievement History "; ?> </span>

<span id="newAchvLink"> <?php echo "<a id='addNewLink' href='addachievement.php?usrid=" . $usrid . "'> Add new</a>"; ?></span>
<br /> <?php echo "<h2> Achv: </h2>  <h3 class='achvtitle'>" . $achvtitle . "</h3>"; echo $achvdescr;?><br /><br />
</body>
</html>

【问题讨论】:

  • 您需要不断地获取行,使用whilestackoverflow.com/questions/12647154/…
  • 使用while循环显示所有行
  • 我不确定在这种情况下如何使用它@Ghost
  • 另外,不要使用@ 运算符来防止错误消息。此外,您的代码容易受到 SQL 注入的攻击。
  • @dowo3 为什么要重新检查你的代码来重构它?立即修复它,以便您在推出软件时节省时间

标签: php html mysql sql


【解决方案1】:
first table query
while loop
{
$userid=first_table_data['user_id'];
2nd table query where userid=$userid
while loop
{

}

all value save in array
}
print value

【讨论】:

    【解决方案2】:

    如果您有 PHP echo 语句,您可以将其替换为...

     while($row = $result->fetch_assoc($response)) {
     echo "<span> " . $username . "s Op Achievement History ";
    

    您应该查看 JOIN 来为您简化所有这些操作。 https://www.w3schools.com/sql/sql_join.asp

    这是一个很好/易于理解的 PHP 循环用法。 https://www.tutorialspoint.com/php/php_loop_types.htm

    【讨论】:

    • OMKARA 有更深入的回答。我还是会看看学习 JOIN 子句。
    【解决方案3】:

    你可以使用while循环来打印所有行

    $query = "SELECT usrid, username, oldname, languages, joindate, art, hunting, frontwebdev, backwebdev, writing, programming, se, smm, pentesting, timezone, 
     availability, reliability, profilePicture FROM userinfo WHERE usrid='" . $usrid . "';";
    $response = @mysqli_query($connection,$query);
    while ($row = @mysqli_fetch_array($response))
    {
        echo $row['username'];
    }
    

    【讨论】:

    • 当我尝试这个时,什么都没有检索到。页面保持空白
    • 删除@并重试
    • 我使用了上面的代码,(你的修改版本,所以它会获取正确的信息)它现在只检索最后/最近的行(具有相关的 id)
    • 尝试回显 $query 并在 phpmyadmin 中运行,看看您的查询是否正常工作
    猜你喜欢
    • 2022-06-23
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2017-12-18
    • 2020-10-20
    相关资源
    最近更新 更多