繁体   English   中英

如何在PHP中使用for循环返回mysql数据

[英]How can I use a for loop in PHP to return mysql data

我正在制作一个将在列表视图中按特定商店列出员工的应用程序。 我当前在DB_Functions.php文件中的函数是:

public function getEmployeeList($name) {
    $stmt = $this->con->prepare("SELECT employee_name FROM employees WHERE name = ?");

    $stmt->bind_param('s', $name);

    if ($stmt->execute()) {
        $employee_list = $stmt->get_result()->fetch_assoc();
        $stmt->close();

        if (empty($employee_list)) {
            return NULL;
        } else {
            return $employee_list;
        }
    }
}

在我的employee.php文件中,我有以下代码:

<?php

require_once 'include/DB_Functions.php';
$db = new DB_Functions();

$response = array('error' => FALSE);

if (isset($_POST['name'])) {
    $name = $_POST['name'];

    $employee_list = $db->getEmployeeList($name);

    if ($employee_list != false) {
        $response['error'] = FALSE;
        //EMPLOYEE LIST OBJECT HERE
    } else {
        $response['error'] = TRUE;
        $response['error_msg'] = 'No employees have been added to this profile.';

        echo json_encode($response);
    }
} else {
    $response['error'] = TRUE;
    $response['error_msg'] = 'You have not logged in to your store\'s account, please log in first.';

    echo json_encode($response);
}

?>

我想在上面的注释空间中有一个employee_list对象。 就像是:

$response['employee_list']['0'] = $employee_list['0'];
$response['employee_list']['1'] = $employee_list['1'];
$response['employee_list']['2'] = $employee_list['2'];

等...等...

将JSONObject返回到android应用后,其内容将在列表视图中列出。 我需要一个for循环( 我认为 ),因为永远不会知道员工编号,因为每个商店都可以根据需要添加和删除员工。 有人可以指出正确的方向,也可以建议我是否在其余代码中使用了正确的方法。 谢谢。

首先,在DB_Functions.php中,您应该返回mysqli_result对象。 因此,您的DB_Functions应该是这样的:

public function getEmployeeList($name) {
    $stmt = $this->con->prepare("SELECT employee_name FROM employees WHERE name = ?");

    $stmt->bind_param('s', $name);

    if ($stmt->execute()) {
        // we get the mysqli_result object without calling the fetch_assoc() on it
        $result = $stmt->get_result();
        $stmt->close();

        // if the count is less than 1, no result found, hence return null
        if ($result->num_rows < 1) {
            return null;
        } else {
            // we return the mysqli_result object without calling the fetch_assoc() on it
            return $result;
        }
    }
}

在您的employee.php中,您想要的是这样的:

<?php

require_once 'include/DB_Functions.php';
$db = new DB_Functions();

$response = array('error' => FALSE);

if (isset($_POST['name'])) {
    $name = $_POST['name'];

    $result = $db->getEmployeeList($name);

    // do an early check for if result returns null or is not set
    if (is_null($result) || !$result) {
        $response['error'] = TRUE;
        $response['error_msg'] = 'No employees have been added to this profile.';
    } else {
        $response['error'] = FALSE;
        //EMPLOYEE LIST OBJECT HERE
        // since $result->fetch_assoc() returns one row at a time, you want to loop through each row and get the appropriate data
        while ($row = $result->fetch_assoc()) {
            // inject the current into the employee_list array
            $response['employee_list'][] = $row;
        }
    }
} else {
    $response['error'] = TRUE;
    $response['error_msg'] = 'You have not logged in to your store\'s account, please log in first.';
}

// echo response gets called no matter what
echo json_encode($response);

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM