duanmei4362
2014-11-24 15:38
浏览 67
已采纳

如何从数据库中检索数据?

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>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douqian2524 2014-11-24 15:45
    已采纳

    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++;
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题