Jump to content

Object Oriented PHP and PDO


RyanMinor

Recommended Posts

Hi everyone,

 

It was recommended that I should make the switch from using procedural code to object oriented code with pdo for queries and such. So, I have been doing some reading on both topics, but am finding it rather hard to wrap my head around. I managed to get a PDO query to work with the help of a tutorial that I found online. My next step is to create an object oriented member registration class. I was wondering if anyone on here could provide any advice as far as the setup goes. I will want my member class to be able to:

1) connect to db

2) gather, validate, and send user data to payment processor

3) receive postback data, insert into database, and notify new member.

 

I have already created these functionalities for other websites using procedural coding so once I get pushed in the right direction I should be find to add the other functionalities, which will include

4) Logging a member in

5) Logging a member out

6) Update member information

7) Change password

8) Forgot password

9) Cancel membership

 

Below is my snippet of PDO code that I wrote. Let me know if you guys would do anything differently. I really appreciate your help in this.

 

<?php
// DECLARE DATABASE CONNECTION VARIABLES //
$host = 'localhost';
$database = 'database';
$username = 'user';
$password = 'password';
// CONNECT TO DATABASE //
try {
$connect = new PDO("mysql:host=$host;dbname=$database", $username, $password);
}
catch(PDOException $e) {
echo $e->getMessage();
}


$sql = "SELECT siteInfoPage, siteInfoKeywords, siteInfoDescription FROM siteInfo WHERE siteInfoPage = '$page'";
// FETCH INTO A PDOStatement OBJECT //
$query = $connect->query($sql);
// ECHO THE NUMBER OF COLUMNS //
$data = $query->fetch(PDO::FETCH_OBJ);
// ASSIGN TO VARIABLES
$page_title =  $data->siteInfoPage;
$keywords = $data->siteInfoKeywords;
$description = $data->siteInfoDescription;

// CLOSE THE DATABASE CONNECTION //
$connect = null;

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.