سؤال

I have been making huge strides in learning Flex and I am very much enjoying it, however, one thing I cannot find much help on is how to bind results from a query in Flex. I have managed to create lists etc no problem but when I try to bind one specific value to a variable, it does not work.

First off, here is my PHP function:

public function getRepnameByUsername($itemID) {

        $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where Username=?");
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'i', $itemID);        
        $this->throwExceptionOnError();

        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        mysqli_stmt_bind_result($stmt, $row->RepID, $row->Username, $row->FirstName, $row->Surname, $row->SerialKey);

        if(mysqli_stmt_fetch($stmt)) {
          return $row;
        } else {
          return null;
        }

    }

Then of course, my function in Flex I have not included all the variables as they are pretty obvious and the only one I am having trouble with is the repServ one declared inside this function.

            protected function btnSubmitUser_clickHandler(event:MouseEvent):void
        {
            username = txtUsername.text;
            password = txtPassword.text;
            serialno = txtSerialNo.text;
            userName.writeUTFBytes(username);
            passWord.writeUTFBytes(password);
            serialNo.writeUTFBytes(serialno);
            EncryptedLocalStore.setItem("Username", userName);
            EncryptedLocalStore.setItem("Password", passWord);
            EncryptedLocalStore.setItem("Serial", serialNo);
            var repServ:Repname = new Repname();
            getRepnameByUsernameResult.token = repnameService.getRepnameByUsername(username);
            repServ = getRepnameByUsernameResult.lastResult;
            repid = repServ.RepID;
            RepID.writeInt(repid);
            EncryptedLocalStore.setItem("Rep", RepID);
        }

The line that is throwing errors is repid = repServ.RepID and I think it is something to do with how I am trying to bind it by the line above. What is the best way of doing this?

Thanks J

هل كانت مفيدة؟

المحلول

I see a few things wrong with your code. First, the remote calls are asynchronous. That means the they run in a different thread. So if we look at this code:

        getRepnameByUsernameResult.token = repnameService.getRepnameByUsername(username);
        repServ = getRepnameByUsernameResult.lastResult;

There is an extremely high probability that the getRepnameByUsername service will not have finished exection, or returned anything, by the time that you try to access the results. Instead you should listen for the result event on the service. Conceptually like this:

        getRepnameByUsernameResult.addEventListener(ResultEvent.RESULT, onMyResult);
        getRepnameByUsernameResult.token = repnameService.getRepnameByUsername(username);

Then in your result handler, you can process the results:

protected function onMyResult(event:ResultEvent):void{
            repServ = getRepnameByUsernameResult.lastResult as Repname;
            repid = repServ.RepID;
            RepID.writeInt(repid);
            EncryptedLocalStore.setItem("Rep", RepID);
}

The second thing I see wrong is that you are not casting the lastResult as an actual object type. I would have expected that to throw a runtime error. I added the conversion in the above code.

You didn't tell us what type of service you're using. ( HTTPService, RemoteObject, or WebService).

It doesn't look like you ever create an object named Repname with the result data. If you are using AMF (RemoteObject), then you can automatically translate the server side object to a client side object. But, you'll probably have to turn your query into that object on the server so it can be translated automatically. If you're using WebService or HTTPService, you'll have to parse the data in the client to turn it into a Repname object.

The results of lastResults are most likely a generic object. You can access specific properties using syntax like this:

getRepnameByUsernameResult.lastResult['repid']

You could either use it as a generic object or convert it manually.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top