我正在尝试使用php从我的数据库中检索数据...目前我有2个提交按钮,您单击以检索数据。一个是带回案例研究,另一个是带回图像。' retrieve.php'代码从名为“P_CASE_STUDIES”的表中检索数据。这很有效。但是,当我尝试从名为' P_IMAGES的第二个表中检索内容时......它没有显示。有人能告诉我我做错了什么。
以下是我的images.php页面的代码:
<?php
function images ()
{
// Connect to the SQL DB
$conn = new SQL_connection("webservice");
$conn->connect();
// Create SQL query
$sql = "SELECT * FROM P_IMAGES";
// Execute query
$result = mysqli_query( $conn->link(), $sql );
// Loop over all result rows
$result_array = array();
while( $post = mysqli_fetch_assoc( $result ) )
{
$result_array[] = $post;
}
// Write to JSON
header( 'Content-type: application/json' );
echo json_encode( $result_array );
}
以下是我的retrieve.php(P_CASE_STUDIES)的代码:
<?php
function retrieve ()
{
// Connect to the SQL DB
$conn = new SQL_connection("webservice");
$conn->connect();
// Create SQL query
$sql = "SELECT * FROM P_CASE_STUDIES;";
// Execute query
$result = mysqli_query( $conn->link(), $sql );
// Loop over all result rows
$result_array = array();
while( $post = mysqli_fetch_assoc( $result ) )
{
$result_array[] = $post;
}
// Write to JSON
header( 'Content-type: application/json' );
echo json_encode( $result_array );
}
网络服务页面
<?php
class Webservice
{
var $link;
//Switch statement to call relevant function
function __construct()
{
require __DIR__."/sql_connection.php";
switch ($_POST['action'])
{
case 'retrieve':
require "retrieve.php";
retrieve();
break;
case 'images':
require "images.php";
images();
break;
default:
echo "Error";
break;
}
}
}
$go = new Webservice();
?>
最后我的index.html:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Pinder</title>
</head>
<body>
<form action="webservice.php" method="POST">
<table>
<tr>
<td><input name="action" value="retrieve" type="hidden"></td>
<td><input type="submit" value="submit" class="button"></td>
</tr>
</table>
</form>
<form action="webservice.php" method="POST">
<table>
<tr>
<td><input name="action" value="images" type="hidden"></td>
<td><input type="submit" value="submit" class="button"></td>
</tr>
</table>
</form>
</body>
</html>
答案 0 :(得分:1)
您每次都使用$ post
替换数组一个问题! P_CASE_STUDIES中实际有多少行?
while( $post = mysqli_fetch_assoc( $result ) )
{
$result_array[] = $post;
}
应该是这样的:
var i = 0;
while( $post = mysqli_fetch_assoc( $result ) )
{
$result_array[$i] = $post;
$i++;
}