I am trying to retrieve data from my database using php... Currently I have 2 submit buttons which you click to retrieve the data. One to bring back case studies and one to bring back images.'retrieve.php' code retrieve's data from a table called 'P_CASE_STUDIES' and this works. However when I try to retrieve the content from the second table called 'P_IMAGES'... it doesn't display. Can someone tell me what im doing wrong.
Here is the code for my images.php page:
<?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 );
}
Here is the code for my 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 );
}
Web services page:
<?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();
?>
And finally my 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>