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

报告相同问题?

悬赏问题

  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52