The interaction of PHP in FLEX3

Source: Internet
Author: User
Tags mysql commands rowcount sprintf tag name xml parser

About February 23, I read an article in the library about how to interact with PHP in FLEX3. I was fascinated because the thought inside was so refined that it was better than FLEX2 many. Visible, it is still being paid attention to, flex or gradually grow.

However, has already said to oneself, did not grind the postgraduate, to except C + + technology all lay down. Put it down. C + + is the most powerful.

The automatically generated code in FLEX3 is logical and insightful and versatile. The first is a PHP file consisting of 2 functions of transformations in the data type (Mysql-->flex): functions.inc.php

<?php/** * Simple escape function for SQL values/function getsqlvaluestring ($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {$theValue = GET_MAGIC_QUOTES_GPC ()? Stripslashes ($theValue): $theValue; $theValue = function_exists ("mysql_real_escape_string")? Mysql_real_escape_string ($theValue): mysql_escape_string ($theValue); Switch ($theType) {case "text": $theValue = ($theValue!= "")? ' ". $theValue. ' "':" NULL "; Break Case "Long": Case "int": $theValue = ($theValue!= "")? Intval ($theValue): "NULL"; Break Case "Double": $theValue = ($theValue!= "")? ' ". Doubleval ($theValue). ' "':" NULL "; Break Case "Date": $theValue = ($theValue!= "")? ' ". $theValue. ' "':" NULL "; Break Case "defined": $theValue = ($theValue!= "")? $theDefinedValue: $theNotDefinedValue; Break return $theValue; function Getsqlvaluestringforselect ($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {$theValue = GET_MAGIC_QUOTES_GPC ()? StriPslashes ($theValue): $theValue; $theValue = function_exists ("mysql_real_escape_string")? Mysql_real_escape_string ($theValue): mysql_escape_string ($theValue); Switch ($theType) {case "text": $theValue = ($theValue!= "")? ' " %' . $theValue. '% ': ' NULL '; Break Case "Long": Case "int": $theValue = ($theValue!= "")? Intval ($theValue): "NULL"; Break Case "Double": $theValue = ($theValue!= "")? ' ". Doubleval ($theValue). ' "':" NULL "; Break Case "Date": $theValue = ($theValue!= "")? ' ". $theValue. ' "':" NULL "; Break Case "defined": $theValue = ($theValue!= "")? $theDefinedValue: $theNotDefinedValue; Break return $theValue; }?>

Then you encapsulate the commonly used MySQL commands (insert deletion updates, and so on) as functions and events in Flex. The generated student.php. It is worth noting that the content of this file is different from your data table, and it is worth to upgrade later, each data table corresponds to a php file, corresponding to its own operation, but this happened a lot of redundancy produced.

It reminds me of the LINQ in my work with Mr. Wang, who feels the same way. Oh, it is no wonder that last night on the CSDN home page to see a cow on the blog, he walked through a lot of languages, now stay on the AS3. AS3 itself is very powerful, if there is a number of threads and C inside some of the characteristics, I will be as attentive to study, oh, but this is my postgraduate after the matter.

The student.php code is as follows:

<?php require_once (DirName (__file__). "/flexphpdbconn.php"); Require_once (DirName (__file__). "/functions.inc.php"); Require_once (DirName (__file__). "/xmlserializer.class.php"); /** * This is the main PHP file and process the HTTP parameters, * performs the basic DB operations (Find, INSERT, UPDATE , DELETE) * and then serialize the response in 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 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 get a 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> * &LT;/ROW&G T * <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 this generated server side code does not have any * specific authentication Mechan ISM in place. * */** * the filter field. This is the ' only field ' that we'll do filtering after. * * $filter _field = "Name"; /** * 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 did a full Sele CT, no Limit * Returns:an array of the form * Array (* Data => Array (* Array (' field1 ' => "value1", "Field2" =&gt ; "value2") * ... *), * metadata => Array (* "Pagenum" => page_index, * "Totalrows" => number_of_rows *) *)/F Unction FindAll () {global $conn, $filter _field, $filter _type/** * The list of fields in the table. We need this to check that the "Sent value for" ordering is indeed correct. * * $fields = array (' id ', ' Name ', ' Sex ', ' age ', ' address ', ' remark '); $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 ' student ' $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,name,sex,age,address,remark from ' Student ' $where $order '; If we use pagination, add the limit clause if ($pageNum >= 0 && $pageSize > 0) {$query _recordset = Sprint F ("%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 resp Onse 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: * $_r equest["Filter"-The filter value * Returns:an array of the form * Array (* data => number_of_rows, * metadata =&G T 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 ' student ' $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; }/** * Constructs and executes a SQL insert query against the selected database * can take the following parameters: * $_ request["Field_name"]-the list of fields which appear here would be used as values for insert. * If A field does not appear, NULL would be used. * Returns:an array of the form * Array (* Data => Array (* "PRIMARY key" => primary_key_value, * "Field1" Value1 "* ... *), * metadata => Array () *)/function Insert () {global $conn;//build and execute the Insert query $query _insert = sprintf (insert INTO ' student ' (Id,name,sex,age,address,remark) VALUES (%s,%s,%s,%s,%s,%s) ", Getsqlvaluestring ($_request["id"], "int"), # getsqlvaluestring ($_request["Name"], "text"), # getsqlvaluestring ($_ request["Sex"], "TexT "), # getsqlvaluestring ($_request[" Age "," int "), # getsqlvaluestring ($_request[" Address "]," text "), # Getsqlvaluestring ($_request["remark"], "text") #); $ok = mysql_query ($query _insert); if ($ok) {//Return the new entry, using the Insert id $toret = Array ("Data" => Array (Array ("id" => $_request["I D "]," name "=> $_request[" name "], #" Sex "=> $_request[" Sex "], #" age "=> $_request[' age '], #" Address "=> $_r equest["Address"], # "remark" => $_request["remark"]#)), "metadata" => Array ()); else {//We had an error, return it $toret = Array ("Data" => Array ("Error" => mysql_error ()), "metadata" => a Rray ()); return $toret; }/** * Constructs and executes a SQL update query against the selected database * can take the following parameters: * $_ Request[primary_key]-thethe value of the primary key * $_request[field_name]-the list of fields which here would is used as values for update. * If A field does not appear, NULL would be used. * RETurns:an array of the form * Array (* Data => Array (* "PRIMARY key" => primary_key_value, * "Field1" => E1 "* ... *), * metadata => Array () *)/function Update () {global $conn;//Check to = If the record actually ex Ists in the database $query _recordset = sprintf ("SELECT * from ' student ' WHERE id =%s", getsqlvaluestring ($_request["id"] , "int")); $recordset = mysql_query ($query _recordset, $conn); $num _rows = mysql_num_rows ($recordset); if ($num _rows > 0) {//Build and execute the update query $row _recordset = MYSQL_FETCH_ASSOC ($recordset); $query _updat E = sprintf ("UPDATE ' student ' SET Name =%s,sex =%s,age =%s,address =%s,remark =%s WHERE id =%s", Getsqlvaluestring ($ _request["Name", "text"), getsqlvaluestring ($_request["Sex"], "text"), Getsqlvaluestring ($_request["age"], "int"), Getsqlvaluestring ($_request["Address"], "text"), getsqlvaluestring ($_request["remark"], "text"), getsqlvaluestring ($row _recordset["id"], "int")); $ok = mysql_query ($query_update); if ($ok) {//Return the updated entry $toret = Array ("Data" => Array (Array ("id" => $row _recordset["id"], "Name" => $_request["Name", # "Sex" => $_request["Sex"], # "age" => $_request[' age '], # "address" => $_request["Addr" ESS "], #" remark "=> $_request[" remark "]#))," metadata "=> Array ()); else {//an update error, return it $toret = Array ("Data" => Array ("Error" => mysql_error ()), "metadata" => a Rray ()); } else {$toret = array ("Data" => Array ("Error" => "No Row Found"), "metadata" => Array ()); }/** * Constructs and executes a SQL update query against the selected database * can take the following parameters: * $_ Request[primary_key]-thethe value of the primary key * Returns:an array of the form * Array (* data => Deleted_row _primary_key_value, * metadata => Array () *)/function Delete () {global $conn;//Check to = if the record actual LY exists in the database $query _recordset = SPRINTF ("select * from ' student ' WHERE id =%s", getsqlvaluestring ($_request["id"], "int")); $recordset = mysql_query ($query _recordset, $conn); $num _rows = mysql_num_rows ($recordset); if ($num _rows > 0) {$row _recordset = Mysql_fetch_assoc ($recordset); $query _delete = sprintf ("Delete from ' student ' whe RE id =%s ", getsqlvaluestring ($row _recordset[" id "]," int ")); $ok = mysql_query ($query _delete); if ($ok) {//delete went through OK, return OK $toret = Array ("Data" => $row _recordset["id"], "metadata" => Array ( ) ); else {$toret = array ("Data" => Array ("Error" => mysql_error ()), "metadata" => Array ());} else {//no row found, return an error $toret = array ("Data" => Array ("Error" => "no Row Found"), "metadata" => Array ()); return $toret; /** * We use this as an error response, if we don't receive a correct method * */$ret = array ("Data" => Array ("Err or "=>" No operation ")," metadata "=> Array ()); /** * Check for the database Connection * * */if ($conn = = False) {$ret = array ("Data" => Array ("Error" => "Database connection error, please check yo ur settings! ")," metadata "=> Array ()); else {mysql_select_db ($database _conn, $conn);/** * Simple dispatcher. The $_request[' method '] parameter selects the operation to execute. * Must be one of the values FindAll, insert, UPDATE, DELETE, Count *//execute the necessary function, according to the Operation code in the post variables switch (@$_request[' method ']) {case "FindAll": $ret = FindAll (); ": $ret = insert (); Break Case "Update": $ret = Update (); Break Case "Delete": $ret = delete (); Break Case "Count": $ret = RowCount (); Break }} $serializer = new XmlSerializer (); echo $serializer->serialize ($ret); Die ();?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.