Jump to content

PHP MySQL Question

- - - - -

  • Please log in to reply
1 reply to this topic

#1
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
I know to get a row from the Database via mySQL & PHP I can use something like
		$db=mysql_connect($DBAddress,$DBUser,$DBPw);
		mysql_select_db('MyDB',$db);
		$LQuery ="SELECT * From myorders WHERE SOID = '$ShipperID' ";
		$QResult = mysql_query($LQuery);
		$ShipperInfo = mysql_fetch_object($QResult);
		$CustomerOrder = utf8_encode($ShipperInfo->CustomerOrder);
		$PhoneNumber = utf8_encode($ShipperInfo->PhoneNumber);
		$Attention = utf8_encode($ShipperInfo->Attention);
... ect

and that by using a class I can have something like
class order{
public $ShippingOrder;
public $CustomerOrder;
public $PhoneNumber;
public $Attention;}

So that the a query might be returned as
  $foundorder = new order;
  $db=mysql_connect($DBAddress,$DBUser,$DBPw);
  mysql_select_db('MyDB',$db);
  $LQuery ="SELECT * From myorders WHERE SOID = '$ShipperID' ";
  $QResult = mysql_query($LQuery);
  $ShipperInfo = mysql_fetch_object($QResult);
  $foundorder->SOID = utf8_encode($ShipperInfo->SOID);
  $foundorder->CustomerOrder = utf8_encode($ShipperInfo->CustomerOrder);
  $foundorder->PhoneNumber = utf8_encode($ShipperInfo->PhoneNumber);
  $foundorder->Attention = utf8_encode($ShipperInfo->Attention);
... ect

But is their a better way to load the results from the fetch_object to the order class object ?
I.E. would
$foundorder = $QResult; work if they both have the same structure?
Also how would one do an Update to the database using a class object ?

$SQLQuery = "UPDATE `myorders` SET '$neworder' WHERE `OrderID`= '$myorders->OrderID' LIMIT 1";

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
If you wish to do this, you could always extend your order class to iterate through the database object, and copy all its members (i.e. with foreach) to the current class.
class order {  public $SOID;


  public function import($order) {
      foreach ($order as $property => $value)
      {
          $this->$property = $value;
      }
  }
}

I would run checks within the foreach to ensure that each member contains content, and also is a member of the current class.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users