PHP OOP Login/Register System: Database (Part 7/23)

PHP OOP Login/Register System: Database (Part 7/23)


Connecting to our database by starting to build our database wrapper, which will eventually allow us to take control of our database and develop database functionality quickly.

Watch the full series at http://www.youtube.com/playlist?list=...

Subscribe to phpacademy and enjoy more free web development tutorials!

Our channel
http://youtube.com/phpacademy

Official website
http://phpacademy.org

Support Forum
http://phpacademy.org/forum

Follow us on Twitter!
http://twitter.com/phpacademy
Closed Caption:

so it's now time to build the core of
our application essentially what we're
going to be accessing database data with
out-system wouldn't work properly
without this we will be able to store
anything and we're going to be building
here is a database rapper and
essentially what this is its neighbors
abstracted way to work without database
and give ourselves easy to work with
functionality now the beauty of this
database rapper is that it can be used
outside of this application as well so
once you finish writing this code you're
going to be able to use this in anything
you do and that's the point really of
building it like this its its code
reusability something that isn't tied
directly to our application we can use
this anywhere we want
now we're going to be using PDO PHP data
objects of the sole reason that we can
actually then define what kind of
database that we want to work with in
this case we're going to be using mysql
because it's so popular but we can of
course change this and work with any
other database that they're supported by
P do so let's go ahead and start to
define our class now we're going to be
testing this along the way in our index
dot PHP file so we're going to be
looking at the kind of things it does
i'm also going to be keeping an eye on
our database so we're gonna be inserting
things extracting things queering
updating looking at security and stuff
like that so this is quite a large thing
to build but once we're done we can
solve relax and know that we can work
really efficiently without database
let's go ahead and define the class this
is just going to DB now we're going to
be working with a pattern in PHP called
the singleton pattern so we're going to
have a main static in a static method
called get instance and this is just
going to allow us to get the an instance
of our database if it's already been if
it's already been instantiated and
basically this means that we don't have
to keep connected to our database again
and again and again on each page we can
just use the database on the fly as we
want
if you were to create a class with a
constructor which will do in a moment
you would be every time you need to you
instantiate your database object to
perhaps create a new query and taking
you set of results would be connected to
your database again and again and again
which is very very inefficient so we're
just going to be using the get instant
static method in this case that doesn't
make sense
don't worry as we build the code it will
make perfect sense hopefully so the
first thing we're going to do is create
I public and I would crap private static
variable called instant instance that's
gonna be now let's give that a
underscore as well so what we've done
here is we've created a private static
property to our object this is going to
store the instance of the database if
it's available if or if it's not
available we're going to stand check our
DB object from within it and store it
here again
sounds complicated but once we get
around this pattern it's going to make
things or things should become a lot
quickly revenge early so we have some
other private properties these only
static properties these just gonna be
private properties without object which
won't be accessible to outside of this
class will be accessible to anything
that extends from this we just need them
to be private
there's no need for them to be public so
we have private video and the underscore
base E is a notation that lets us know
that as we're using these they are
private properties and they're not
they're not public or protect well
protected reduce underscored too so we
have a PDO query not explaining these in
just a moment
error
results and count going to set count 20
i'm going to set ever folks and we can
bring these down line-by-line to just
make it look a bit Nita things make a
little bit more sense
so what's happening here is we've got p
do this is going to represent the when
we when we instantiate the PDO object
we're going to store here so we can use
elsewhere query is going to be the last
query that's executed every is going to
wrap them whether there's an error or
not so whether the query filed or not
and will have a method to check your
return whether there has been an error
we have results which will store our
results set so if we were to pick say
all users with the name the first name
Alex that will be a resultset say
there's 10 users that will be stored
there and the count of results which is
extremely important because also going
to provide a method that's going to
allow us to say you know is their own it
have there been any results returned or
something like that so basically the way
that we're going to use our database
class is going to be similar to this so
it's really useful to take a look at
what we actually want our database
object to do and then we can build it so
for example let's say we want to grab a
list of all users the way we want this
to work is we want to have a basically
the ability to just say get me a certain
amount of data so quite simply we could
say something like users equals $OPERAND
DB get instant now this is going to be
the the method the static method that
we're going to create as part of the
singleton object so what we will cover
this in detail in a moment and all that
then say something like query select
username from users so that's it
now what we can do is say something like
if users count that would actually
probably be a method that would use so
that's quite as a method and then we can
say for each user's as user echo user
username and because we're going to be
returning an object this basically
represents the field name in a database
so that would be that would represent
this so this is the way that we're going
to be building things now there's a lot
more functionality than this but you can
see you looking at this it makes it a
lot easier to work with your date space
rather than having to you know Connect
PDA at the top of your page
xq query blah blah and we can basically
do anything like this and we're gonna
have a lot of helper methods as well
they're going to allow us to define
things in a certain way so for example
we could say get from users and we could
say yes so we can define something like
where username equals Alex so we're
basically going to be building this
functionality and it makes it so easy to
work with your database I mean look at
that it's all breeds like a book get you
get users where username equals Alex or
username is greater than 50 it wouldn't
be username with it it would be
something like . i don't know it you can
use this for a variety of of purposes so
that's what we're basically going to be
doing so let's go ahead and and create
our constructor function and this is did
all this is going to do is connect to
our database so let's go ahead and
create a private method this will be
construct and this is run when the cost
since instantiate
did they were going to try the PDO
connection i'm going to catch a PDO
error so this is basically a try-catch
block which allows you to basically
catch errors inside of the try block so
if if we do have an error with basically
just going to kill the application and
then output message that's returned from
this and when we are working without our
connection will will mess around with
the settings and see how this affects it
but we're going to do is we're going to
set the PDO private PDO property to the
PDO connections we're going to say new
PDO and this is just standard syntax you
can find this in in the PHP
documentation so what the way the space
it looks as it's a string then we have a
username and a password the initial
string defines what what database
basically handler or whatever however
you want to call it you want to connect
to and then it defines the database name
as well and the host obviously so in
this case we're connected to a mysql
database the host is going to be basic
the host that we define and I'm just
gonna put X's and Y's and says here
because we're going to use our conflict
class that we created to actually handle
this
so the host is going to be 127 . not one
the DB name is going to be the database
name that we defined and then we have a
username and we have a password so we'll
put said an a for example so for the
hosts let's go ahead and just
concatenate on a value this is going to
be config get mysql host as we saw when
we created our our conflict here so this
is basically my skewer host so the
database name is going to be a convict
get
isql ed Templars that and then for the
username and the password we already
know that we've store these in the
conflict they are here and here so we
just do exactly the same thing convict
get mysql user name and exactly the same
here
let's just go ahead and just copy and
paste this change this to password so
that's it that's out that's a line of
out of connecting to our database
let's go ahead and give this a go just
by saying DB was new DB and we'll look
at $TIME and will look at sorting this
out in just a moment in fact this is not
going to work and this is the whole
point of the singleton pattern we need
to call DB instance so if we were to say
DB get instance we have the well we
create the public static method and
that's going to be called getinstance
now what this is going to do it's going
to check if we've already instantiate an
object by instantiating object with
creating were connecting to our database
if we have an instantiated with we're
going to instantiate it if we have we're
going to return the instance so in this
case we need a check here to say if the
instance is not set so that self member
of dealing with static properties here
so we need to use self and scope
resolution operator here i'm going to
say self instance if that's not been set
we want to actually go ahead and set
that
and UD be sorry
so what this is going to do is if this
isn't set which by default it won't be
when we using this on say one page we
want to go ahead and create new DB
instance that's going to run this set P
do here and it's then going to return
self instance so we can start using all
of the functionality of our class or
object so if in the case that we use
this twice on a page this one no longer
be valid so this will this will return
false and all will do is just return the
instance that are commonly working with
we can create a new query we just don't
have to reconnect our database so let's
take a look at and sort of prove how
this actually works so let's refresh so
we don't have an arrow we we know now
that we are more than likely just using
the right credentials but if we were to
say root said or something like that
we got him a fresh you can see that we
we kill the page and we we get the error
back there so we know that we're
connecting successfully to our database
or we we can safely assume that we are
let's take a look at what happens here
so if i go ahead and echo connected here
that will put that inside of a try block
so let's go ahead and refresh so we say
connected now because we're where we
would ordinarily be running the
constructor function every time we
instantiate this class create an object
from it we assume that if we were to do
this then we would see this message
again and again and again but because
we're first calling getinstance we're
checking whether this has been set or
not
ye were checking whether this private
construct method has been called and
we've actually connected to our database
so when i refresh now refreshing the
page we only connect once now if this
was to be reversed and we had this as a
public function and we were to ignore
the fact that we were to get instance
and we were to say something like DB
equals new DB and do this over and over
and over and over again what were then
doing is if we were to use this down a
page you know a few times
we're connecting to a deck space again
and again and again there's no need to
be only need to we need to store one
connection one of link to our database
if you like
so by doing this we actually avoid the
need to having over to have to even
accidentally connect to our database
again and again and again
so we've connected to our database
really what's left to do now is just go
over the that the useful methods that we
need to create inside of our database to
be able to pull data and actually
manipulate data update things delete
records and things like that

Video Length: 15:02
Uploaded By: Codecourse
View Count: 81,665

Related Software Products
Web Log DB
Web Log DB

Published By:
ExactTrend

Description:
The Web Log DB exports web log data to databases via ODBC. Web Log DB uses ODBC to export data from raw log files to your database, using SQL queries. Web Log DB allows you to use the applications you have become accustomed to such as MS SQL, MS Excel, MS Access etc. Also, any other ODBC compliant application can now be used to produce the output you desire. Use Web Log DB to perform further analysis in special softs. Web Log DB supports more than 25 log file formats. It was tested with all ...


Related Videos
Create Login Form Access Database Part 1
Create Login Form Access Database Part 1

Create Login Form Access Database Part 1 Download sample Access file at : http://www.iaccessworld.com/?p=1304 ==========Attention!! Please================== There was an error on some code. you can fix it by combining criteria LoginID and Password in one criteria under one DLookup. The fixed code is "If (IsNull(DLookup("[UserLogin]", "tblUser", "[Userlogin] ='" & Me.txtLoginID.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Then msgbox ...
Video Length: 15:25
Uploaded By: austin72406
View Count: 467,416

Java prog#3. Login Program for java with sqlite [ Mysql ] database PART-1
Java prog#3. Login Program for java with sqlite [ Mysql ] database PART-1

Donate to ProgrammingKnowledge -- (https://www.paypal.com/de/cgi-bin/web...) JPasswordfield The sql connect jar file can be downloader form this link below Creating Login Page In Java using NetBeans code for login using netbeans Java Netbeans Login Building a Login Screen for a NetBeans NetBeans Platform Login Tutorial create login page contain user id,password,buttons in java using netbeans NetBeans Platform Login ...
Video Length: 10:01
Uploaded By: ProgrammingKnowledge
View Count: 336,718

ASP.NET Tutorial 3- How to Create a Login website - Creating Database For website
ASP.NET Tutorial 3- How to Create a Login website - Creating Database For website

Setting up the Database ASP.NET Site Building ASP.NET Web Applications with ASP.NET Web Pages Databases ASP.NET Web Forms Tutorial After creating Website using ASP.NET (c#), sqlserver(Database SQL server DAtabase Creating a Database ASP.NET Web Pages Tutorial Create a Database Application in 15 Minutes with ASP.NET Create a website with ASP.Net Create an ASP.NET website ASP.NET Web Pages Databases ASP.NET Web Forms Database
Video Length: 13:42
Uploaded By: ProgrammingKnowledge
View Count: 311,880

ASP.NET Tutorial 6- Create a Login website - Login page & Validating User and Password in database
ASP.NET Tutorial 6- Create a Login website - Login page & Validating User and Password in database

asp.net login validation with database c# - Validating username and password in a database in asp.net Validating User Input in ASP.NET Web Pages Sites Asp.Net Examples: Simple login form example in asp.net How to create Login page/form and check username,password in database Simple login form example in asp.net Check Username How to implement form validation using ASP.Net 2.0 How to Validate User Login in asp.net using DataSet ASP.NET Form ...
Video Length: 22:58
Uploaded By: ProgrammingKnowledge
View Count: 300,093

ASP.NET Tutorial 4- How to Create a Login website - Connecting Database to Registration Page
ASP.NET Tutorial 4- How to Create a Login website - Connecting Database to Registration Page

ASP.NET Database Connection ASP.NET Web Forms Database How to connect asp.net to a database Creating sql database connection in asp.net web application How to make database connectivity in asp.net Make connection to database in ASP.NET c# - asp.net database connection connect jqgrid to database using WebSecurity.InitializeDatabaseConnection() to CREATE DATABASE permission denied in database How to connect asp.net to a databasebr ...
Video Length: 25:50
Uploaded By: ProgrammingKnowledge
View Count: 285,534

Login system using PHP with MYSQL database
Login system using PHP with MYSQL database

In this video, i create a table users with fields id, username and password. Then i inserted a user whose credentials i used to login.
Video Length: 18:30
Uploaded By: Awa Melvine
View Count: 249,133

PHP Tutorials: Register & Login (Part 1): Database
PHP Tutorials: Register & Login (Part 1): Database

Download the full series and complete source code at https://phpacademy.org/course/registe... Setting up the database table for our users, so we can store their information. This will allow us to process their login using their username and password, and store any additional information about them. Register & Login series playlist is available at: http://www.youtube.com/playlist?list=... Official website http://phpacademy.org Support Forumbr ...
Video Length: 08:58
Uploaded By: Codecourse
View Count: 249,076

Netbeans Java Login Application + MySQL
Netbeans Java Login Application + MySQL

Tutorial Java Netbeans Login Application with MySQL database
Video Length: 28:48
Uploaded By: Fajar Sebastian
View Count: 150,771

ASP.NET Web Forms website Part 1 - Connect with MySQL database connection
ASP.NET Web Forms website Part 1 - Connect with MySQL database connection

ASP.NET Website creation example using some limited HTML, ASP.NET tags, C# code files, and connecting to a MySQL database. Here I did a step by step demonstration on how to create a semi functional ASP.NET Website. There's no heavy emphasis on HTML, there is no CSS, however I do show how to add textboxes, buttons to a web page and then tie the web application to MySQL Database for authentication. If you want to see how to harden the Web Forms login page against SQL ...
Video Length: 45:40
Uploaded By: Chris Duran
View Count: 119,193

Login form in Netbeans and MySQL as Database
Login form in Netbeans and MySQL as Database

Log-in using user name and password stored in the database. For jdbc jar file Open the link and click on download link : http://dev.mysql.com/downloads/file.php?id=13598
Video Length: 12:32
Uploaded By: CoderNut Tutorials
View Count: 104,746

Copyright © 2025, Ivertech. All rights reserved.