gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:07 24 4
gpt4 key购买 nike

我正在制作一个应用程序,它将在 ListView 中按特定商店列出员工。我的 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;
}
}
}

在我的 employees.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 应用程序后,内容将列在 ListView 中。我需要一个 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;
}
}
}

在你的 employees.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);

希望对你有帮助

关于php - 如何在 PHP 中使用 for 循环返回 mysql 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35969387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com