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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?