NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
NuSOAP and MySQL


Joined: 04 Oct 2006
Posts: 2
Reply with quote
Hi all,

I am getting by feet wet on web services and found nuSoap, which seems like a very cool tool. I followed an example at codewalkers.com (http://codewalkers.com/tutorials/74/1.html) which worked very well. I then proceed to modify that example a bit further to retrieve data from the database dynamically (you enter information in the form on the soap client and get the result back based on the parameter you entered). I was able to retrieve a single record from the database, but not multiple records. When I tried the php code (without soap) to retrieve data from the database, I was able to retrieve multiple records, so it seems like the SQL statement is correct. If so, why is it not working with nuSoap? Any suggestions on what I am doing wrong?

The soap server is as follows:

Code:

<?php
function getStockQuote($id) {

    mysql_connect('host','username','password');
    mysql_select_db('db');
    $query = "SELECT * FROM table "
           . "WHERE id = '$id' ";
    $result = mysql_query($query);

    while ($row = mysql_fetch_assoc($result)){
    return $row['address'];
    }
}

require('lib/nusoap.php');

$server = new soap_server();

$server->configureWSDL('server', 'urn:stockquote');

$server->register("getStockQuote",
                array('symbol' => 'xsd:string'),
                array('return' => 'xsd:string'),
                'urn:stockquote',
                'urn:stockquote#getStockQuote');

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)
                      ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>


What I am trying to do is retrieve all addresses associated with the id that is entered in the form on the soap client. But it is only returning the first record in the table.

If I try just the following SQL statement in a php file, it return all records:

Code:
 
   mysql_connect('host','username','password');
    mysql_select_db('db');
    $query = "SELECT * FROM table "
           . "WHERE id = '$id' ";
    $result = mysql_query($query);

    while ($row = mysql_fetch_assoc($result)){
    echo $row['address'];
    }



The soap client for the above server is:

Code:

<html>
<body>

<form method="get" action="client.php">
  ID: <input name="symbol" type="text" value="">
  <br>
  <br>
  <input type="submit">
</form>

<?php

$symbol = $_GET['symbol'];

if ($symbol) {

require_once('lib/nusoap.php');

//we create an array with the element name that has the form value of name

//now we must create a soapclient object
$c = new soapclient('http://domain.com/server.php');
//now we call the server.

$stockprice = $c->call('getStockQuote',
              array('symbol' => $symbol));

echo "Information for $symbol is $stockprice.";


}

?>


</body>
</html>



So, it seems that I am doing something incorrectly in the soap server.

Thanks for your help.

Ray[/code]
View user's profileFind all posts by raymasaSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8342
Reply with quote
if you want to return or pass as argument(s) anything other than simple data types like integers or strings you have to register corresponding complex types.
See "examples" topic that contains references to good sources.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 04 Oct 2006
Posts: 2
Reply with quote
Hi dmitri,

Thanks for the response. I just need to return simple data type (strings) in this case, so I am not sure if I still need to register complex types. What I was trying to do is return multiple records, but instead I am only getting a single record in return. I think (thou, I could be wrong), it’s the SQL statement in the soap server that is causing the problem. However, I can’t figure out where the problem is because when I use the SQL statement by itself in a php file (not with nuSoap), I get multiple records returned.

To give an example, my database table is:

Code:

    Id       Stock
    1        IBM
    1        MSFT
    1        ORCL
    2        IBM
    2        CSCO


So, if in the client I enter id = 1, I should get the results as follows:

Code:

   IBM
   MSFT
   ORCL

However, what I am getting back is:

Code:

   IBM



That is, only the first record in the database, instead of all the records for that id.

Hope my explanation is a bit clear. Based on this should I still register complex types? Or would the way I have setup the soap server is fine? (sorry, I am all very new to this).

In the soap server, my SQL query is as follows:

Code:


function getStockQuote($id) {

    mysql_connect('host','username','password');
    mysql_select_db('db');
    $query = "SELECT * FROM table "
           . "WHERE id = '$id' ";
    $result = mysql_query($query);

    while ($row = mysql_fetch_assoc($result)){
    return $row['address'];
    }
}



Shouldn’t the while loop return all the records for the given ID? It does so, when I use the above SQL in a PHP file (without soap), but with soap, its only returns the first record.

Any ideas?

Thanks for your help.

Ray
View user's profileFind all posts by raymasaSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8342
Reply with quote
of course you have to register returning complex type as array.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website
NuSOAP and MySQL
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic