Php 如何从数据库中检索数据?
我正在尝试使用php从数据库检索数据。。。目前,我有两个提交按钮,您可以单击它们来检索数据。一个用于带回案例研究,另一个用于带回图像。“retrieve.php”代码从一个名为“P_case_studies”的表中检索数据,这很有效。但是,当我尝试从第二个名为“P_IMAGES”的表中检索内容时。。。它不显示。有人能告诉我我做错了什么吗 以下是my images.php页面的代码:Php 如何从数据库中检索数据?,php,html,database,hybrid-mobile-app,Php,Html,Database,Hybrid Mobile App,我正在尝试使用php从数据库检索数据。。。目前,我有两个提交按钮,您可以单击它们来检索数据。一个用于带回案例研究,另一个用于带回图像。“retrieve.php”代码从一个名为“P_case_studies”的表中检索数据,这很有效。但是,当我尝试从第二个名为“P_IMAGES”的表中检索内容时。。。它不显示。有人能告诉我我做错了什么吗 以下是my images.php页面的代码: <?php function images () { // Connect to the SQL
<?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 );
}
<!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>
最后是我的index.html:
<?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 );
}
<!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>
品德
您每次都用$post替换阵列
一个问题!P_案例研究中实际有多少行
while( $post = mysqli_fetch_assoc( $result ) )
{
$result_array[] = $post;
}
应该是这样的:
var i = 0;
while( $post = mysqli_fetch_assoc( $result ) )
{
$result_array[$i] = $post;
$i++;
}
它不显示
。它没有显示什么?您可以添加更多的echo()语句来查看脚本和查询是如何执行的。如果数组为空,json_encode()的结果会是什么?我想你还是会收到一些东西。@greenapps看到的是我不知道在哪里回音。。。我是php新手!这段代码是由来自Internet的源代码组合而成的。您可以将echo()语句放在需要的地方。它就像Print()或Log.d(),当然不需要i。但您可以在调试时echo()查看行数。@greenapps在其他地方有一个上载图像的函数。。。行永远不会停止。不,我刚刚发现代码很好。。。它只是无法检索图像的,因为文件太大。。。现在开始研究如何解决这个问题。我希望你能解决这个问题:互联网是你的朋友:D@TheGarrett. 请不要将此答案标记为正确。正如你自己所说,这与你的问题无关。最好自己回答,并记下。