如何在PHP中使用for循环返回mysql数据
我正在制作一个应用程序,可以在列表视图中列出某个商店的员工。我的DB_Functions.php文件中的当前函数是:如何在PHP中使用for循环返回mysql数据,php,mysql,Php,Mysql,我正在制作一个应用程序,可以在列表视图中列出某个商店的员工。我的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->
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);
}
?>
等等。。。等等
将JSONObject返回到android应用程序后,内容将在列表视图中列出。我需要一个for循环(我想),因为员工编号永远不会被知道,因为每个商店都可以根据自己的意愿添加和删除员工。是否有人能为我指出正确的方向,并建议我在代码的其余部分是否使用了正确的方法。谢谢。首先,在DB_Functions.php中,您应该返回mysqli_result对象。 因此,您的DB_函数应该是:
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;
}
}
}
公共函数getEmployeeList($name){
$stmt=$this->con->prepare(“从名称=?”的员工中选择员工的姓名”);
$stmt->bind_参数('s',$name);
如果($stmt->execute()){
//我们获取mysqli_result对象,而不调用其上的fetch_assoc()
$result=$stmt->get_result();
$stmt->close();
//如果计数小于1,则找不到结果,因此返回null
如果($result->num_rows<1){
返回null;
}否则{
//我们返回mysqli_result对象,而不对其调用fetch_assoc()
返回$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);
首先,在DB_Functions.php中,应该返回mysqli_结果对象。
因此,您的DB_函数应该是:
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;
}
}
}
公共函数getEmployeeList($name){
$stmt=$this->con->prepare(“从名称=?”的员工中选择员工的姓名”);
$stmt->bind_参数('s',$name);
如果($stmt->execute()){
//我们获取mysqli_result对象,而不调用其上的fetch_assoc()
$result=$stmt->get_result();
$stmt->close();
//如果计数小于1,则找不到结果,因此返回null
如果($result->num_rows<1){
返回null;
}否则{
//我们返回mysqli_result对象,而不对其调用fetch_assoc()
返回$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);
非常感谢您+1.我设法将结果输入到应用程序中,我可以在日志中看到如下内容:{“error”:false,“employee_list”:[{“employee_name”:“Jason Matthews”},{“employee_name”:“Tori Wilson”}}}现在,我只需要弄清楚如何将所有这些内容放入可选择的列表中。非常感谢你的帮助。非常感谢+1.我设法将结果输入到应用程序中,我可以在日志中看到如下内容:{“error”:false,“employee_list”:[{“employee_name”:“Jason Matthews”},{“employee_name”:“Tori Wilson”}}}现在,我只需要弄清楚如何将所有这些内容放入可选择的列表中。非常感谢你的帮助。