如何显示姓名和电子邮件?

时间:2019-03-17 13:58:56

标签: php mysql

使用此代码,我将显示找到的用户数,但我也想显示姓名和电子邮件。请改进我的代码。如何显示计数行的所有详细信息?我不知道你能帮我么?

<?php
$conn=mysqli_connect("localhost","root","","mlm");
$i = 1;
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="select count('$username') from user where under_referral = '$username'";

$result=mysqli_query($conn,$sql);

$row=mysqli_fetch_array($result);

// display records in a table
echo '<div style="overflow-x:auto;">';
echo "<table>";
// set table headers
echo "<tr><th>Level</th><th>User Count</th></tr>";
echo "<tr>";
echo "<th>$i</th><th>" .$row[0]. "</th><th><a href='level1.php'>View</a> </th>";

echo "</tr>";
$i++;
echo "</table>";
echo '</div>';
mysqli_close($conn);
?>

2 个答案:

答案 0 :(得分:0)

首先,通过查询一次选择所需的所有内容:

$sql="select name, email from user";

然后使用:

遍历您的结果
while ($row = mysqli_fetch_array($result)) { [...]

最后从每一行获取数据:

$name = $row['name'];

如果需要,您可以使用:

$number_of_rows = $result->num_rows;

答案 1 :(得分:0)

很简单:

$sql="select count('$username') as count,name,email from user where under_referral = '$username'";