使用nymber_格式通过mysql和php检索员工工资

使用nymber_格式通过mysql和php检索员工工资,php,mysql,localhost,Php,Mysql,Localhost,我使用php从musql数据库返回员工信息。我使用number\u format方法以格式化逗号返回工资,但返回数据时,所有员工的工资都相同。如何让php从employees表返回个人工资 PHP/MySQL <?php require_once("db.php"); $sql = "SELECT `*` FROM `employees`"; $results = mysqli_query($connect, $sql) or die(mysql_erro

我使用php从musql数据库返回员工信息。我使用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>");
    ?>

那是因为你的工资在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>");
    ?>
现在,你应该做的是:跟随评论

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

希望有帮助。

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

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

…他没有将该函数应用于数组,而是应用于数组中第一行工资的一项。但是你的代码是对的!Idk否决了你的答案,但我肯定投了更高的票。这里有些人是仇恨者。@WCA是的,我知道,但我不在乎向下投票,这对你有帮助,将来可能会帮助一些人,这对我有好处……是的,我的也有向下投票,我不知道为什么:Pablo Gonzalez,我明白了,我只能在int进入while循环并被输出后格式化int?