Hello there!
I am new with PDO and I am trying to replace the old code with PDO...
The main issue i have is that i dont know how to get/use the objects vs the arrays when it comes to showing data.
Please see bellow examples and let me know if you can help.
CURRENT/OLD CODE:
class customer
{
function getCustomer($customer_id)
{
$Sql = "SELECT * FROM customer WHERE customerID=".mysql_real_escape_string($customer_id);
$Res = @mysql_query($Sql);
$Row = @mysql_fetch_array($Res);
$this->Name = ucfirst(($Row['Name']));
$this->Email = ucfirst(($Row['Email']));
return $Row;
}
}
and in order to use the class i do the following:
//initiate the class
$Customer = new customer;
$Customer -> getCustomer($_GET[customer_id]);
echo $Customer->Name;
echo $Customer->Email;
i also use the code this way when i need forms:
<input name="customer_name" type="text" value="<?=$Customer->Name?>" />
SO...NEED SOME HELP BELOW...
I am learing PDO and i am trying to replace the old code (like the one above) with the new one, but i am not sure i am doing the right things...please see bellow:
NEW CODE:
//db coonection
$db = new PDO("mysql:host=$db_host;dbname=$db_name",$db_user,$db_pass);
class customer
{
function __construct($db)
{
$this->db = $db;
}
function getCustomer($customer_id)
{
$Sql = "SELECT * FROM customer WHERE customerID=".mysql_real_escape_string($customer_id);
$rs = $this->db->query($sql);
//$row = $rs->fetch(PDO::FETCH_ASSOC);
$row = $rs->fetch(PDO::FETCH_OBJ);
return $row;
}
}
//initiate the class
$Customer = new customer;
question:
1-how can i use the Name and Email as the example shown above?
could I the following?
//initiate the class
$Customer = $Customer -> getCustomer($_GET[customer_id]);
echo $Customer->Name;
echo $Customer-Email;
How do you guys use it?
Sorry if i am not very clear! :)