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

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

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++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改