0 Replies Latest reply on Jun 18, 2009 10:59 AM by kmdguy

    How can I modify this script to return only certain rows of my mySQL table?

    kmdguy

      Hi there,

       

      I have a php script that accesses a mySQL database and it was generated out of the Flex Builder wizard automatically. The script works great and there are no problems with it. It allows me to perform CRUD on a table by calling it from my Flex app. and it retrieves all the data and puts it into a nice MXML format.

       

      My question, currently when I call "findAll" to retrieve all the data in the table, well, it retrieves ALL the rows in the table. That's fine, but my table is starting to grow really large with thousands of rows.

       

      I want to modify this script so that I can pass a variable into it from Flex so that it only retrieves the rows that match the "$subscriber_id" variable that I pass. In this way the results are not the entire table's data, only the rows that match 'subscriber_id'.

       

      I know how to pass a variable from Flex into php and the code on the php side to pick it up would look like this:

       

      $subscriberID = $_POST['subscriberID'];

       

      Can anyone shed light as to the proper code modification in "findAll" which will take my $subscriberID variable and compare it to the 'subscriber_id' field and then only return those rows that match? I think it has something to do with lines 98 to 101.

       

      Any help is appreciated.

       

      <?php

      require_once(dirname(__FILE__) . "/2257safeDBconn.php");

      require_once(dirname(__FILE__) . "/functions.inc.php");

      require_once(dirname(__FILE__) . "/XmlSerializer.class.php");

       

       

      /**

      * This is the main PHP file that process the HTTP parameters,

      * performs the basic db operations (FIND, INSERT, UPDATE, DELETE)

      * and then serialize the response in an XML format.

      *

      * XmlSerializer uses a PEAR xml parser to generate an xml response.

      * this takes a php array and generates an xml according to the following rules:

      * - the root tag name is called "response"

      * - if the current value is a hash, generate a tagname with the key value, recurse inside

      * - if the current value is an array, generated tags with the default value "row"

      * for example, we have the following array:

      *

      * $arr = array(

      *      "data" => array(

      *           array("id_pol" => 1, "name_pol" => "name 1"),

      *           array("id_pol" => 2, "name_pol" => "name 2")

      *      ),

      *      "metadata" => array(

      *           "pageNum" => 1,

      *           "totalRows" => 345

      *      )

      *      

      * )

      *

      * we will get an xml of the following form

      *

      * <?xml version="1.0" encoding="ISO-8859-1"?>

      * <response>

      *   <data>

      *     <row>

      *       <id_pol>1</id_pol>

      *       <name_pol>name 1</name_pol>

      *     </row>

      *     <row>

      *       <id_pol>2</id_pol>

      *       <name_pol>name 2</name_pol>

      *     </row>

      *   </data>

      *   <metadata>

      *     <totalRows>345</totalRows>

      *     <pageNum>1</pageNum>

      *   </metadata>

      * </response>

      *

      * Please notice that the generated server side code does not have any

      * specific authentication mechanism in place.

      */

       

       

       

      /**

      * The filter field. This is the only field that we will do filtering after.

      */

      $filter_field = "subscriber_id";

       

      /**

      * we need to escape the value, so we need to know what it is

      * possible values: text, long, int, double, date, defined

      */

      $filter_type = "text";

       

      /**

      * constructs and executes a sql select query against the selected database

      * can take the following parameters:

      * $_REQUEST["orderField"] - the field by which we do the ordering. MUST appear inside $fields.

      * $_REQUEST["orderValue"] - ASC or DESC. If neither, the default value is ASC

      * $_REQUEST["filter"] - the filter value

      * $_REQUEST["pageNum"] - the page index

      * $_REQUEST["pageSize"] - the page size (number of rows to return)

      * if neither pageNum and pageSize appear, we do a full select, no limit

      * returns : an array of the form

      * array (

      *           data => array(

      *                array('field1' => "value1", "field2" => "value2")

      *                ...

      *           ),

      *           metadata => array(

      *                "pageNum" => page_index,

      *                "totalRows" => number_of_rows

      *           )

      * )

      */

       

      function findAll() {

           global $conn, $filter_field, $filter_type;

       

           /**

            * the list of fields in the table. We need this to check that the sent value for the ordering is indeed correct.

            */

           $fields = array('id','subscriber_id','lastName','firstName','birthdate','gender');

       

           $where = "";

           if (@$_REQUEST['filter'] != "") {

                $where = "WHERE " . $filter_field . " LIKE " . GetSQLValueStringForSelect(@$_REQUEST["filter"], $filter_type);     

           }

       

           $order = "";

           if (@$_REQUEST["orderField"] != "" && in_array(@$_REQUEST["orderField"], $fields)) {

                $order = "ORDER BY " . @$_REQUEST["orderField"] . " " . (in_array(@$_REQUEST["orderDirection"], array("ASC", "DESC")) ? @$_REQUEST["orderDirection"] : "ASC");

           }

           

           //calculate the number of rows in this table

           $rscount = mysql_query("SELECT count(*) AS cnt FROM `modelName` $where");

           $row_rscount = mysql_fetch_assoc($rscount);

           $totalrows = (int) $row_rscount["cnt"];

           

           //get the page number, and the page size

           $pageNum = (int)@$_REQUEST["pageNum"];

           $pageSize = (int)@$_REQUEST["pageSize"];

           

           //calculate the start row for the limit clause

           $start = $pageNum * $pageSize;

       

           //construct the query, using the where and order condition

           $query_recordset = "SELECT id,subscriber_id,lastName,firstName,birthdate,gender FROM `modelName` $where $order";

           

           //if we use pagination, add the limit clause

           if ($pageNum >= 0 && $pageSize > 0) {     

                $query_recordset = sprintf("%s LIMIT %d, %d", $query_recordset, $start, $pageSize);

           }

       

           $recordset = mysql_query($query_recordset, $conn);

           

           //if we have rows in the table, loop through them and fill the array

           $toret = array();

           while ($row_recordset = mysql_fetch_assoc($recordset)) {

                array_push($toret, $row_recordset);

           }

           

           //create the standard response structure

           $toret = array(

                "data" => $toret,

                "metadata" => array (

                     "totalRows" => $totalrows,

                     "pageNum" => $pageNum

                )

           );

       

           return $toret;

      }

       

      /**

      * constructs and executes a sql count query against the selected database

      * can take the following parameters:

      * $_REQUEST["filter"] - the filter value

      * returns : an array of the form

      * array (

      *           data => number_of_rows,

      *           metadata => array()

      * )

      */

      function rowCount() {

           global $conn, $filter_field, $filter_type;

       

           $where = "";

           if (@$_REQUEST['filter'] != "") {

                $where = "WHERE " . $filter_field . " LIKE " . GetSQLValueStringForSelect(@$_REQUEST["filter"], $filter_type);     

           }

       

           //calculate the number of rows in this table

           $rscount = mysql_query("SELECT count(*) AS cnt FROM `modelName` $where");

           $row_rscount = mysql_fetch_assoc($rscount);

           $totalrows = (int) $row_rscount["cnt"];

           

           //create the standard response structure

           $toret = array(

                "data" => $totalrows,

                "metadata" => array()

           );

       

           return $toret;

      }