Flex& PHP - 在Php服务中插入查询

时间:2012-02-16 03:37:14

标签: php flex flash-builder

我有一个带有2个表的MySql DB:
1.类别 - categoryID / category / description
2.照片 - photoID / categoryID / photodescription / photo

我有一个由Flash Builder自动创建的服务(CategoryService.php),但我无法获得第二个表!!

    <?php

class CategoryService {

    var $username = "root";
    var $password = "";
    var $server = "localhost";
    var $port = "3306";
    var $databasename = "teste";
    var $tablename = "category";

    var $connection;

    /**
     * The constructor initializes the connection to database. Everytime a request is 
     * received by Zend AMF, an instance of the service class is created and then the
     * requested method is invoked.
     */
    public function __construct() {
        $this->connection = mysqli_connect(
                                $this->server,  
                                $this->username,  
                                $this->password, 
                                $this->databasename,
                                $this->port
                            );

        $this->throwExceptionOnError($this->connection);
    }

    /**
     * Returns all the rows from the table.
     *
     * Add authroization or any logical checks for secure access to your data 
     *
     * @return array
     */
    public function getAllCategory() {

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

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

        $rows = array();

        mysqli_stmt_bind_result($stmt, $row->categoryId, $row->category, $row->description);

        while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->categoryId, $row->category, $row->description);
        }

        mysqli_stmt_free_result($stmt);
        mysqli_close($this->connection);

        return $rows;
    }

    /**
     * Returns the item corresponding to the value specified for the primary key.
     *
     * Add authorization or any logical checks for secure access to your data 
     *
     * 
     * @return stdClass
     */
    public function getCategoryByID($itemID) {

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

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

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

        mysqli_stmt_bind_result($stmt, $row->categoryId, $row->category, $row->description);

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

    /**
     * Returns the item corresponding to the value specified for the primary key.
     *
     * Add authorization or any logical checks for secure access to your data 
     *
     * 
     * @return stdClass
     */
    public function createCategory($item) {

        $stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (category, description) VALUES (?, ?)");
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'ss', $item->category, $item->description);
        $this->throwExceptionOnError();

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

        $autoid = mysqli_stmt_insert_id($stmt);

        mysqli_stmt_free_result($stmt);     
        mysqli_close($this->connection);

        return $autoid;
    }

    /**
     * Updates the passed item in the table.
     *
     * Add authorization or any logical checks for secure access to your data 
     *
     * @param stdClass $item
     * @return void
     */
    public function updateCategory($item) {

        $stmt = mysqli_prepare($this->connection, "UPDATE $this->tablename SET category=?, description=? WHERE categoryId=?");      
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'ssi', $item->category, $item->description, $item->categoryId);       
        $this->throwExceptionOnError();

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

        mysqli_stmt_free_result($stmt);     
        mysqli_close($this->connection);
    }

    /**
     * Deletes the item corresponding to the passed primary key value from 
     * the table.
     *
     * Add authorization or any logical checks for secure access to your data 
     *
     * 
     * @return void
     */
    public function deleteCategory($itemID) {

        $stmt = mysqli_prepare($this->connection, "DELETE FROM $this->tablename WHERE categoryId = ?");
        $this->throwExceptionOnError();

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

        mysqli_stmt_free_result($stmt);     
        mysqli_close($this->connection);
    }


    /**
     * Returns the number of rows in the table.
     *
     * Add authorization or any logical checks for secure access to your data 
     *
     * 
     */
    public function count() {
        $stmt = mysqli_prepare($this->connection, "SELECT COUNT(*) AS COUNT FROM $this->tablename");
        $this->throwExceptionOnError();

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

        mysqli_stmt_bind_result($stmt, $rec_count);
        $this->throwExceptionOnError();

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

        mysqli_stmt_free_result($stmt);
        mysqli_close($this->connection);

        return $rec_count;
    }


    /**
     * Returns $numItems rows starting from the $startIndex row from the 
     * table.
     *
     * Add authorization or any logical checks for secure access to your data 
     *
     * 
     * 
     * @return array
     */
    public function getCategory_paged($startIndex, $numItems) {

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

        mysqli_stmt_bind_param($stmt, 'ii', $startIndex, $numItems);
        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        $rows = array();

        mysqli_stmt_bind_result($stmt, $row->categoryId, $row->category, $row->description);

        while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->categoryId, $row->category, $row->description);
        }

        mysqli_stmt_free_result($stmt);     
        mysqli_close($this->connection);

        return $rows;
    }


    /**
     * Utility function to throw an exception if an error occurs 
     * while running a mysql command.
     */
    private function throwExceptionOnError($link = null) {
        if($link == null) {
            $link = $this->connection;
        }
        if(mysqli_error($link)) {
            $msg = mysqli_errno($link) . ": " . mysqli_error($link);
            throw new Exception('MySQL Error - '. $msg);
        }       
    }
}

?>

在Flash Builder中,我有2个spark列表,第一个列出了所有的类别,我希望selectedItem用相应的照片填充另一个。{1}} 我有一个与数据库一起使用的查询但我无法将其插入到php中。 这是查询:

SELECT TC.*, TP.photodescription , TP.photo FROM category TC inner join photos TP on TC.categoryId = TP.categoryId

我该怎么做!? 希望这是可以理解的,感谢“倾听”。

编辑:

<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.FlexEvent;

        import spark.events.IndexChangeEvent;

        protected function list_creationCompleteHandler(event:FlexEvent):void
        {
            getAllCategoryResult.token = categoryService.getAllCategory();
        }


        protected function list2_creationCompleteHandler(event:FlexEvent):void
        {
            getAllphotosResult.token = photosService.getAllphotos();
        }

        protected function list_changeHandler(event:IndexChangeEvent):void
        {
            category = list.selectedItem;
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <s:CallResponder id="getAllCategoryResult"/>
    <categoryservice:CategoryService id="categoryService"
                                     fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
                                     showBusyCursor="true"/>
    <valueObjects:Category id="category"/>
    <s:CallResponder id="getAllphotosResult"/>
    <photosservice:photosService id="photosService"
                                   fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
                                   showBusyCursor="true"/>
</fx:Declarations>
<s:List id="list" x="197" y="127" creationComplete="list_creationCompleteHandler(event)" change="list_changeHandler(event)"
        labelField="category">
    <s:AsyncListView list="{getAllCategoryResult.lastResult}"/>
</s:List>
<s:List id="list2" x="708" y="127" creationComplete="list2_creationCompleteHandler(event)"
        labelField="photo">
    <s:AsyncListView list="{getAllphotosResult.lastResult}"/>
</s:List>

这是我的.mxml的一个例子           我怎样才能在“selectedItem”中根据categoryId更改第二个List!?     你能帮忙或给我一些例子吗?      再次感谢..

1 个答案:

答案 0 :(得分:0)

FLashBuilder为您创建了一个CRUD(创建,读取,更新,删除)类。这仅基于您的一个类别表。 var $tablename = "category"; - 因此它只返回类别的结果。如果你想做一个JOIN,那么你有几个选择:

  1. 自定义使用JOIN语句在生成的类中编写新方法,从FlashBuilder项目中删除该类,并将更新后的文件作为现有PHP类导入FlashBuilder。

  2. 为照片表创建第二个生成的PHP类,然后在Flex应用程序中加入结果。

  3. 使用“合并字段”选项进行JOIN操作,该操作返回从多个数据库表中检索的数据。 (可能是最好的选择)

  4. 要详细了解如何执行此操作,请参阅:http://help.adobe.com/en_US/Flex/4.0/AccessingData/WSbde04e3d3e6474c4-668f02f4120d422cf08-7ff7.html