Jump to content

amfphp general help


ReggieTheDog
Go to solution Solved by Ch0cu3r,

Recommended Posts

New to php and even newer to amfphp so consider me an amoeba when it comes to coding... I'm not a developer. I'm just someone who decided to jump in at the deepend one day..

I originally created  a php script to to query a mySQL database using PDO. The first query grabs the data and the second query gets the found rows. The script then does its xml magic and sends it to the requesting flash AS3 app.

So, last week I found out about amfphp and became intrigued. It took me that week to understand how to get basic data out of a database and trace it in flash but I got there. So, now I want to reproduce the above in an amfphp service class.. I think I'm ok with the main query and the medoths send the data back to Flash with -

return $stmt->fetchAll(PDO::FETCH_ASSOC); 

Worked great. I was so pleased with myself. Then I tried to add a second query to get the total rows and this si where I struggled. In my original script I just  ran the main query looped though it for xml output, then ran the second query and appended the result to the xml... but that doesn't seem to work here.

$sqlQ = "SELECT all my data";
$stmt = $this->con->prepare($sqlQ);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);

$sqlQ = "SELECT FOUND_ROWS()";
$stmt = $this->con->query($sqlQ);
return = $stmt->fetchColumn(0);

And it failed....

Is it not OK to have two return statements in a method? What approach do I need to take to get this to work... I'm completely in the dark and as a blind man in the world of PHP I'm looking for someone with at least one eye to guide me.

 

Link to comment
Share on other sites

Correct, you can't have 2 returns in the same function and expect any data to exist or processing to happen after the function encounters the first return. So in this instance, you would need to combine the data and then return it only once.

 

There are better ways to go about this by using a more advanced db wrapper class that would automatically do the second query and have that info available to you in a number of ways after the fact. Or make your original query run a sub query to get all the found rows and store them in a temp column in the query result.

Link to comment
Share on other sites

 

Is it not OK to have two return statements in a method?

No. return will terminate the function and return the data at that point. The second return statement will never be reached. See the documentation on return here http://php.net/return

 

Any, way you do not need the second query. You could pass the result of calling your function to count to get the total results, this is because $stmt->fetchAll() returns a multidimensional array of all the rows returned by your query. Example

function yourFunction()
{
    ... omitted code ... 
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

// when calling your function
$results = yourFunction();

// pass the result of your function to count, get the number of results returned from your function
$numResults = count($results);

// loop though results
foreach($results as $row)
{
    // generate xml
}

The alternative is instead of using  return $stmt->fetchAll(PDO::FETCH_ASSOC);  to have it return the PDO Statement object  return $stmt;  then when calling your function the code will be

// when calling your function
$stmt = yourFunction();

// call the PDO Statement rowCount() method to get the number of results
$numRows = $stmt->rowCount();

// call the PDO Statement fetchAll method to loop through the results
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $row)
{
    // generate xml
}
Link to comment
Share on other sites

 

No. return will terminate the function and return the data at that point. The second return statement will never be reached. See the documentation on return here http://php.net/return

 

Any, way you do not need the second query. You could pass the result of calling your function to count to get the total results, this is because $stmt->fetchAll() returns a multidimensional array of all the rows returned by your query. Example

function yourFunction()
{
    ... omitted code ... 
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

// when calling your function
$results = yourFunction();

// pass the result of your function to count, get the number of results returned from your function
$numResults = count($results);

// loop though results
foreach($results as $row)
{
    // generate xml
}

The alternative is instead of using  return $stmt->fetchAll(PDO::FETCH_ASSOC);  to have it return the PDO Statement object  return $stmt;  then when calling your function the code will be

// when calling your function
$stmt = yourFunction();

// call the PDO Statement rowCount() method to get the number of results
$numRows = $stmt->rowCount();

// call the PDO Statement fetchAll method to loop through the results
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $row)
{
    // generate xml
}

@Ch0cu3r - Thanks for getting back to me and giving me some guidance. stumbled upon rowCount two mnths ago when moving from mySqli to PDO. The reason I wanted the 'select found rows'  is that I am only bringing the the first 6 rows back in the first query but also need the total row count of the table. Sorry, looking back at my question I didn't really make that clear. Once I understand how to do this I'll be able to rewrite all my php pages that use xml to return resuls in AMF ( oh what joy)... So, how would I be able to do this?

 

Fastsol's suggestion of a DB wrapper sounds good but I think it's a bit beyond me...

Edited by ReggieTheDog
Link to comment
Share on other sites

  • Solution

 

I am only bringing the the first 6 rows back in the first query but also need the total row count of the table. Sorry, looking back at my question I didn't really make that clear.

Yeah, I thought you wanted to get the total rows returned by your query.

 

To hve your fuction return the results from both query, you will have your function return an array. Example

function yourFunction()
{
    ... omitted code ...
    $sqlQ = "SELECT all my data";
    $stmt = $this->con->prepare($sqlQ);
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    $sqlQ = "SELECT FOUND_ROWS()";
    $stmt = $this->con->query($sqlQ);
    $totalRows = $stmt->fetchColumn(0);

    // return the results of queries above as an array
    return array($results, $rowsFound);
}

Then when calling your function you do something like this,

list($results, $rowsFound) = yourFunction();

// or alternatively written as
$data = yourFunction();
$results = $data[0];
$rowsFound = $data[1];
Link to comment
Share on other sites

 

Yeah, I thought you wanted to get the total rows returned by your query.

 

To hve your fuction return the results from both query, you will have your function return an array. Example

function yourFunction()
{
    ... omitted code ...
    $sqlQ = "SELECT all my data";
    $stmt = $this->con->prepare($sqlQ);
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    $sqlQ = "SELECT FOUND_ROWS()";
    $stmt = $this->con->query($sqlQ);
    $totalRows = $stmt->fetchColumn(0);

    // return the results of queries above as an array
    return array($results, $rowsFound);
}

Then when calling your function you do something like this,

list($results, $rowsFound) = yourFunction();

// or alternatively written as
$data = yourFunction();
$results = $data[0];
$rowsFound = $data[1];

@Ch0cu3r - Ah ha... I see said the blind man..... Great I'm off to give it a try. Thank you so much for answering and clarifying what is almost certainly a dumb question. It looks so simple and yet it never occured to me.  I appreciate your time and thoughts.

Edited by ReggieTheDog
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.