How do I retrieve data from my database?

user4287330

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>
Johan Brännmar

You are replacing the array each time with $post

A question! How many rows are there actually in P_CASE_STUDIES?

while( $post = mysqli_fetch_assoc( $result ) ) 
{

    $result_array[] = $post;

}

Should be something like:

var i = 0;
while( $post = mysqli_fetch_assoc( $result ) ) 
{

   $result_array[$i] = $post;
   $i++;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I refresh my activity after deleting data from database (using DAO)

How do i use getIntent().getSerializableExtra to retrieve data from the database

How do I retrieve the last inserted value in my database?

Hibernate: how do I retrieve my entities from a ScrollableResults?

How do I get/retrieve multiple data from uid in the firebase database in android?

Firebase: How do I retrieve records from my data for which a specific key exists?

How can I retrieve data from Firebase to my adapter

how do i retrieve data from firebase realtime database using kotlin

How do I retrieve non-empty & non-duplicate data from the database?

How can I retrieve data from many child in firebase database

How do i retrieve data from Async storage and use it in my component?

Android How to sort data that i retrieve from Database

How do I retrieve the preferences from my settings activity in Kotlin?

How do I retrieve an encrypted password from my database with bcrypt?

How can I retrieve data from the database when the date > now()

How do most android applications retrieve data from a database server?

How do I retrieve data from my dynamic textboxes?

How do I retrieve an image from folder using the database?

How do I retrieve data from firebase database Storage and displaying it into custom listview?

how to retrieve select options from my database

How do I retrieve all the data in my redis instance?

how can I retrieve data from my backend in a table with reactjs?

How do I write an @if in Laravel that uses data from my database?

How can I retrieve Data from a database by using the URL

Haskell Persistent Library - How do I get data from my database to my frontend?

How can I retrieve data from my PostgreSQL database? WPF/C#

Retrieve data from the name of a directory in my database

How do I retrieve the child of the child of the child's data from my firebase Realtime database?

How do I pass data from my html flask to my sqlite3 database(python)