使用nymber\u格式用mysql和php检索员工工资

col17t5w  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(429)

我使用php从musqldb返回员工信息。我使用number\u format()方法将带格式逗号的薪资返回,但返回数据时,所有员工的薪资都相同。如何让php从employees表返回个人工资?
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>");
    ?>
aiqt4smr

aiqt4smr1#

那是因为你的薪水在foreach循环之外(顺便说一句,你把第一行从table上去掉了)。
在我建议你怎么做之前,让我解释一下你的实际代码是怎么回事:(按照注解)

<?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>");
    ?>

希望有帮助。

slmsl1lt

slmsl1lt2#

数字格式在数组上不起作用。这样做:

<?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>");
?>

相关问题