将数据从数据库加载到文本框

时间:2015-05-10 14:10:50

标签: javascript php

我有一个名为'employees'的mysql数据库表 它有3列名为'id','name'和'salary' 我有html / php网页,文本框名为id和name。 我想加载具有最高工资的员工的“id”和“name”,从数据库到这些文本框。

1 个答案:

答案 0 :(得分:0)

 <?php
//Defining constants for database connection best to store in seperate file and include that file
    const DB_HOST = 'SERVER';
        const DB_USER = 'USER';
        const DB_PASS = 'PASSWORD';
        const DB_NAME = 'php_mysql_login_system';
    //Connecting to the database -- best use pdo
        $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        # check connection
        if ($mysqli->connect_errno) {
            echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
            exit();
        }


        $sql = " SELECT * from `employees` "; // query data from databse -- alhough I suggest you use pdo

        $result = $mysqli->query($sql);

        //Looping through each  results

        foreach ($result as  $value) {

            // Store the employess salary and name in an array say employee['name'=>salaray] -- name as key and salary as value
        }

        $maximumSalary = max(//array of the employee i.e the one you have made above employee[]);

        //after getting the maximum value use array_keys(array,value) to get the employee name

        $employeeName = array_keys($employee,$maximumSalary);

    }
    ?>      

让您入门的示例代码希望这对您有所帮助,尽管此代码可能没有遵循最佳实践,因此您至少可以开始使用某些内容,因为我认为您无法开始使用。