我正在使用php从musql数据库返回员工信息。我正在使用number_format()方法以带格式的逗号返回薪水,但是在返回数据时,所有员工的薪水都相同。如何获取php以从employee表中返回个人薪水?
PHP / MySQL
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
$salary = $row['salary'];
$rows_sal = number_format($salary);
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
答案 0 :(得分:0)
number_format在数组上不起作用。这样做:
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
//$salary = $row['salary'];
//$rows_sal = number_format($salary);
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . number_format($row['salary']) . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
答案 1 :(得分:-1)
那是因为您正在使用foreach循环之外的薪水。 (而且,顺便说一句,您将表的第一行遗漏了。)
在我建议您如何做之前,让我解释一下您的实际代码所发生的情况:(遵循注释)
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
//here you're asking THE FIRST row:
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
$salary = $row['salary'];
//so $rows_sal is the first salary. All this have nothing to do with the iteration below.
$rows_sal = number_format($salary);
echo("<table>");
//here you're asking for the next results (leaving 1st row outside the table!)
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
//and here you're referencing the value set before the while.
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
现在这是您应该做的:(关注评论)
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
//don't ask for results yet!
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH)) //including 1st row
{
//now $row is EACH row, NOW you can format your salary:
$salary = $row['salary'];
$rows_sal = number_format($salary);
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
希望有帮助。