Friday, 14 December 2012

PHP - Web Services: Fetching Data From Database

PHP Web Services Tutorial: Now, we try fetch data from database at server side. Send to client as array format. Before read this post, we must understand about sending array format in nusoap. You can read it at here. If you still don't understand about php webservices, you can read this base article.

For practice, we will build simple database about book. Follow this steps:

    Open your phpmyadmin by pointing your browser to http://localhost/phpmyadmin.
    Create new database baru, named "testdb".

    Create new table, named "book".

    Fill fields of this table.

    Insert some example data. Click insert tab.

    Then, write & insert some example data.
    Now, create file named "server_book.php" within www/test/nusoap.

    Enter following code:
    01    <?
    02    //call library
    03    require_once ('lib/nusoap.php');
    04   
    05    //using soap_server to create server object
    06    $server = new soap_server;
    07   
    08    //register a function that works on server
    09    $server->register('getallbook');
    10   
    11    // create the function
    12    function getallbook()
    13    {
    14       $conn = mysql_connect('localhost','root','admin');
    15       mysql_select_db('testdb', $conn);
    16       
    17       $sql = "SELECT * FROM book";
    18       $q  = mysql_query($sql);
    19       while($r = mysql_fetch_array($q)){
    20         $items[] = array('cd'=>$r['cd'],
    21                              'title'=>$r['title'],
    22                              'author'=>$r['author'],
    23                           'publisher'=>$r['publisher']);
    24       }
    25       return $items;
    26   
    27    }
    28   
    29    // create HTTP listener
    30    $server->service($HTTP_RAW_POST_DATA);
    31   
    32    exit();
    33   
    34    ?>
    Create a file named "client_book.php" as client within www/test/nusoap. Enter following code:
    01    <?
    02    require_once ('lib/nusoap.php');
    03   
    04    $client = new soapclient('http://localhost:8048/test/nusoap/server_book.php');
    05   
    06    $response = $client->call('getallbook');
    07   
    08    if($client->fault)
    09    {
    10       echo "FAULT: <p>Code: (".$client->faultcode.")</p>";
    11       echo "String: ".$client->faultstring;
    12    }
    13    else
    14    {
    15       $r = $response;
    16       $count = count($r);
    17       ?>
    18        <table border="1">
    19        <tr>
    20           <th>Code</th>
    21           <th>Title</th>      
    22           <th>Author</th>      
    23           <th>Publisher</th>      
    24        </tr>
    25        <?
    26        for($i=0;$i<=$count-1;$i++){
    27       ?>
    28        <tr>
    29           <td><?=$r[$i]['cd']?></td>
    30           <td><?=$r[$i]['title']?></td>
    31           <td><?=$r[$i]['author']?></td>              
    32           <td><?=$r[$i]['publisher']?></td>      
    33        </tr>
    34        <?
    35       }
    36       ?>
    37        </table>
    38        <?
    39    }
    40    ?>

Now, point your browser to http://localhost/test/nusoap/client_book.php. You should get like this:

No comments:

Post a Comment