0 Replies Latest reply on Apr 9, 2012 7:23 AM by saariko

    help downgrade a php script generated by FB back to the old Mysql way

    saariko

      hey all

       

      i am a Flex developer who has written server side stuff with php scripts that were auto-generated by Flash Builder. These scripts use the Mysqli extention, which is not avilable on my shared hosting server. These scripts implement simple CRUD functions to a MySql database. Anyone who has used the data wizard will recognize these scripts.


      Just before i pay more bucks and move to a VPS server in order to enable Mysqli, is there anyone out there who can help me translate this script BACK to the old mysql way?


      much appriciated, Saar

       

       

      <?php

       

      class ClientService {

       

          var $username = "******";

          var $password = "******";

          var $server = "localhost";

          var $port = "3306";

          var $databasename = "rtdbcbx_saar";

          var $tablename = "clients";

       

          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 getAllClient() {

       

              $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->ClientID, $row->First, $row->Last, $row->Phone, $row->Email, $row->Chain, $row->Branch, $row->Kiosk, $row->SubmitDate);

       

              while (mysqli_stmt_fetch($stmt)) {

                $row->SubmitDate = new DateTime($row->SubmitDate);

                $rows[] = $row;

                $row = new stdClass();

                mysqli_stmt_bind_result($stmt, $row->ClientID, $row->First, $row->Last, $row->Phone, $row->Email, $row->Chain, $row->Branch, $row->Kiosk, $row->SubmitDate);

              }

       

              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 getClientByID($itemID) {

       

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

              $this->throwExceptionOnError();

       

              mysqli_stmt_bind_param($stmt, 'i', $itemID);       

              $this->throwExceptionOnError();

       

              mysqli_stmt_execute($stmt);

              $this->throwExceptionOnError();

       

              mysqli_stmt_bind_result($stmt, $row->ClientID, $row->First, $row->Last, $row->Phone, $row->Email, $row->Chain, $row->Branch, $row->Kiosk, $row->SubmitDate);

       

              if(mysqli_stmt_fetch($stmt)) {

                $row->SubmitDate = new DateTime($row->SubmitDate);

                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 createClient($item) {

       

              $stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (First, Last, Phone, Email, Chain, Branch, Kiosk, SubmitDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");

              $this->throwExceptionOnError();

       

              mysqli_stmt_bind_param($stmt, 'ssssssss', $item->First, $item->Last, $item->Phone, $item->Email, $item->Chain, $item->Branch, $item->Kiosk, $item->SubmitDate->toString('YYYY-MM-dd HH:mm:ss'));

              $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 updateClient($item) {

       

              $stmt = mysqli_prepare($this->connection, "UPDATE $this->tablename SET First=?, Last=?, Phone=?, Email=?, Chain=?, Branch=?, Kiosk=?, SubmitDate=? WHERE ClientID=?");     

              $this->throwExceptionOnError();

       

              mysqli_stmt_bind_param($stmt, 'ssssssssi', $item->First, $item->Last, $item->Phone, $item->Email, $item->Chain, $item->Branch, $item->Kiosk, $item->SubmitDate->toString('YYYY-MM-dd HH:mm:ss'), $item->ClientID);     

              $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 deleteClient($itemID) {

       

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

              $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 getClient_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->ClientID, $row->First, $row->Last, $row->Phone, $row->Email, $row->Chain, $row->Branch, $row->Kiosk, $row->SubmitDate);

       

              while (mysqli_stmt_fetch($stmt)) {

                $row->SubmitDate = new DateTime($row->SubmitDate);

                $rows[] = $row;

                $row = new stdClass();

                mysqli_stmt_bind_result($stmt, $row->ClientID, $row->First, $row->Last, $row->Phone, $row->Email, $row->Chain, $row->Branch, $row->Kiosk, $row->SubmitDate);

              }

       

              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);

              }      

          }

      }

       

      ?>