{SOLVED} multiple rows data retrieval in SOAP and PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kencana
    New Member
    • Nov 2006
    • 26

    {SOLVED} multiple rows data retrieval in SOAP and PHP

    Hi all,

    I am a new bie in SOAP and PHP.I got one question about the data retrieval.
    I am able to retrieve the data successfully from my database. this is my sql statement:
    select roadname from address where postalcode Like '123%';

    based on that sql query it should return me multiple rows value, but instead of that, it only return me the first row of the data.i can't see the rest of it..
    I hope somebody can help me out from this problem.
    thank you for your guidance...

    the following is how i configure my SOAP server:
    function getQuote($symbo l){
    mssql_connect ("server","user ","password ");
    mssql_select_db ("database") ;

    $query = "select roadname from address where postalcode Like '123%' ";
    $result = mssql_query($qu ery);

    while($course = mssql_fetch_ass oc($result))
    {
    $courses= array('Road' => $course["roadname"]);
    }

    foreach ($courses as $road =>$name)
    {
    return $name;
    }
    }

    }

    while the following is my SOAP client:
    $client = new SoapClient("sto ckquote3.wsdl);

    try
    {
    $result= ($client->getQuote($symb ol));


    echo "Your Address is:\n";
    //print ($client->getQuote($symb ol));
    echo $result;
    echo "<br>\n";

    regards,
    Kencana
  • vssp
    Contributor
    • Jul 2006
    • 268

    #2
    while($course = mssql_fetch_ass oc($result))
    {
    $courses= $course["roadname"];}


    Hi frinde replace this code Then try

    vssp

    Comment

    • kencana
      New Member
      • Nov 2006
      • 26

      #3
      hi,

      I have try it out, but still it only return me the first row of the data,
      the rest of the data didn't showed...
      any idea of this issue?

      regards,
      Kencana

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        I really wish people would adhere to some advice and READ THE POSTING GUIDELINES AT THE TOP OF THIS FORUM!! Being a newbie is no excuse.
        So next time you post any code, put it between code,php or html tags!!

        • each time you find a matching value, you replace the content of $courses with the new found value. So $courses always only contains 1 value.
        • you must use $courses as an array, so you use the square brackets to get the following entry in the array.
        • I don't know if it is on purpose, but you return only 1 array ($name) from the function and it is an array containing another array. What do you do when you have more then ONE results? Remember, it only returns 1 of the results with the return statement.


        To get the code working as you have it now, just add the square brackets in the $courses assignment, like this:

        [php]
        $courses = array();
        while($course = mssql_fetch_ass oc($result)) {
        $courses[]= array('Road' => $course["roadname"]);
        [/php]
        Ronald :cool:

        Comment

        • kencana
          New Member
          • Nov 2006
          • 26

          #5
          Hi Ronald,

          Thank you for the explanation and I am sorry for the mistake I made.
          will try to work on it..and once again thank you for the explanation

          Regards,
          Kencana

          Comment

          • kencana
            New Member
            • Nov 2006
            • 26

            #6
            hi Ronald,

            I have try it and instead of giving me the exact result (e.g. road name) it return me "Array".I have attach my code on it, and it is in php code.I really don't have any idea where I went wrong.I do really hope you can help me out.

            Regards,
            Kencana

            this is my SOAP client side code:

            <?php
            $symbol=$_GET['address'];
            $client = new SoapClient("sto ckquote3.wsdl", array(
            "trace" => 1,
            "exceptions " => 0));
            try
            {
            $result= $client->getQuote($symb ol);
            echo "Your Address is:\n";
            echo $result;
            echo "<br>\n";
            }
            catch (SoapFault $exception)
            {
            echo "wrong user";
            }
            //print ($client->getQuote($symb ol));
            ?>

            and this is my SOAP server code:
            <?php
            class QuoteService {
            function getQuote($symbo l){

            if ($symbol=='')
            {
            return "invalid postal code";
            }
            else
            {
            mysql_connect ("localhost","r oot","password" );
            mysql_select_db ("sample");

            $query = "SELECT saddress from example WHERE spostal LIKE '$symbol%'";
            $result = mysql_query($qu ery);

            $courses = array();

            while($course = mysql_fetch_ass oc($result))
            {
            $courses[]= array('Road_nam e' => $course["saddress"]);
            return $courses;
            }
            }
            }
            //ini_set("soap.w sdl_cache_enabl ed", "0"); // disabling WSDL cache
            $server = new SoapServer("sto ckquote3.wsdl") ;
            $server->setClass("Quot eService");

            $server->handle();
            ?>

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              You get an array as the result, so you must treat it as an array! And you don't do that!

              Anyway, this code you show is different from the one you requested help for.

              Ronald :cool:

              Comment

              • kencana
                New Member
                • Nov 2006
                • 26

                #8
                Hi Ronald,

                Do you have any sample code regarding using PHP and SOAP to retrieve multiplerow data for me to refer?

                Many Thanks,
                Kencana

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  Ok next time you display your code without enclosing them in php, code or html tags, I will not reply to your post any more! It takes me too much time and energy to go through all your very unstructures code!

                  1. What is the following statement doing?[php]while($course = mysql_fetch_ass oc($result))
                  {
                  $courses[]= array('Road_nam e' => $course["saddress"]);
                  return $courses;
                  }[/php]
                  You want to read all found in a while loop, but you return only one row immediately after read? What is the point here?

                  2. As I said before, you pass an array back to your caller, so the caller must treat it as an array. See the following simplifed example and look at the statements nr 8 and 9, that is where the returned result array is printed out. So when you have 1 result, you get 1 displayed. If you have 6 results, 6 are displayed.[php]<?php
                  $symbol="123";
                  $result= getQuote($symbo l);
                  if count($result) == 0)
                  echo 'No results found';
                  else {
                  echo "Address(es ) found:\n<br>";
                  for ($i=0;$i<count( $result);$i++)
                  echo "{$result[$i]['Road_name']}<br>\n";
                  }

                  function getQuote($symbo l){

                  $courses = array();
                  mysql_connect ("localhost","u ser","psw");
                  mysql_select_db ("db");
                  $query = "SELECT roadname from address WHERE postalcode LIKE '$symbol%'";
                  $result = mysql_query($qu ery);
                  while ($course = mysql_fetch_ass oc($result))
                  $courses[] = array('Road_nam e' => $course["roadname"]);
                  return $courses;
                  }
                  ?>[/php]
                  3. This treatment of your result has nothing to do with SOAP. It is just proper handling of the returned result(s).

                  Ronald :cool:

                  Comment

                  • kencana
                    New Member
                    • Nov 2006
                    • 26

                    #10
                    Hi Ronald,

                    First of all sorry for the mistake I have done, and secondly I want to say thank you very much for the solution you gave.It works perfectly...
                    Thank you.

                    Regards,
                    Kencana

                    Comment

                    Working...