Connect to MySQL with PHP in XAMPP / Create a new database
This video will discuss how to create a database using PhpMyAdmin and connect to that database using PHP- mysqli. Syntax is discussed as well as some best practices. There a re many outdated tutorials which use mysql - these should not be used interchangeably.
Closed Caption:
hello and welcome in this video I'm
going to cover how to connect to my
sequel database using PHP and so you can
see I'm using xampp here i'm just going
to work off the control panel
so if you have xampp you should be
probably doing like this i'm going to
click on the admin button next to my
sequel but this is going to do is going
to get me over to PHP my admin
now if you are working with a live host
you probably have this tool available to
you or something similar is just your
job to find it when you were free host
or something you might not but if you're
paying for roasting you probably have
this tool and just your job to find it
so really I said we're going to connect
to a database we need to do that we need
to create a database first I mean I've
got some over here but let's create one
so i'm gonna click on the databases tab
and we'll call it test DB great basic as
it gets
just going to go with the default
collation which is basically how the
physical data is represented on disk so
I create that and now I've got a
database called test DB now i can
connect to this database now if I was
really doing something I would set up
users and passwords and everything like
that but i really want to talk about
xampp for a second here
xam to say that security is an
afterthought is not really accurate
xampp is disabled every security what
you can
so when you connect to a database you
can have a username and a password the
default username and password for the
super user is root and no password so
that's relevant because i'm going to go
here and create a kinect script
so here i am a notepad plus plus i'm
going to write a little PHP here just
opening and closing tags
nothing yet since so really there's
three variables which we're going to be
concerned about the first one is a user
now as I said yeah in xampp it's just
route
that's the that's the username you can
create more if you have a live web site
it's not going to be rude right there
would be a big mistake if you were
really had the username fruit
the other thing is going to be the
password and that is literally just
blank and so in xampp they disabled
everything they can disable just to get
you up and running and testing your site
you can adjust these things but by
default they're not there and the last
variable is just the name of the
database
mine was test DB you're feeling a little
PHP syntax here
it's not the friendliest stuff in the
world but that's what it looks like and
really so the magic part here is just
one line and so this connection is very
well going to be used multiple times so
I'm going to store it in a variable such
as this and really what I want to do is
I want to create a new mysqli and they
lived here is a function and i'm going
to call that function so that function
takes for argument's the way i'm going
to use it the first one is localhost
now this is going to work on X am this
would likely work on a web server as
well it might seem like its it would
only work on my computer because i'm
running exhale but that's not really the
case
what that really means is that the my
sequel server is on the same servers as
a patchy and it is in some cases if it's
not you might have to do some kind of an
absolute link right there
the next piece of information is the
username and the last is the password
sorry and the database and so you can
see why we used variables here because
if you have a real website out there
just substitute whenever you use the
name is substitute whatever your
password is if you don't know what that
is
check your email has typically like as
soon as you pay for something you get an
email with all these you know usernames
and passwords is probably in there or
get a hold of support that in theory
should work but it's good practice to
right or we're going to call function
called I alright so what Die means is if
this piece right here
if the left hand of this expression
doesn't go well
some reason i have a syntax error then
it's going to die and i just terminate
the program right there and it spits out
a message like unable to connect it will
tell it will spit out whatever is held
spit out
and so it's either going to be
successful or it's going to die
and at this point I think it's time to
save my file
i'm going to call it connect dot PHP
notice the file extension is gonna yell
at me cause I already have one
I just tried to make this video in her
to go with the audio didn't work
I think I replaced it and let's see if
it works
so I'm going to run it nothing happens
nothing should happen because i need to
run it through the local host write PHP
is not going to be interpreted if i
don't know i run through local host and
nothing happens that's a little
concerning
so I recall what this test is either
creates a connection or it fails there's
no output so it's good practice to just
do a little echo down here and Michael's
going to say like a great work great
little enthusiasm layer and so that
might look like it would happen every
time but in theory it's either going to
successfully create the connection or
die right now if in in the case that it
dies it's never going to get to this
line because it just stops right there
so i'll save this and you'll see why
didn't admit it so I refresh and great
work
read the implication that it didn't die
and i didn't get any warnings so it
probably worked so let's let's see what
it looks like let's say the user did not
know a password
all right save that i refresh and she
not so great work i still get the great
work because it didn't technically died
i just write access denied so it's a
warning warnings don't warnings are in a
complete failure warnings are just
they're just bad and you should address
them and if its kind of sticking out to
your thinking my gosh that seems kind of
weird to the password is just in plain
text well it is and there's all kinds of
things you can do to make that unless
difficult situation or school right i
mean you can do a better job than what
we're doing here
but in theory all you got to do is
substitute in the values that you want
and so at this point you should be able
to connect your database so this this is
how it is by default in xampp you want
to create users or you know you've got a
live web site with some real user names
and
passwords all you gotta do is substitute
that's the kind of beauty of writing it
out like this and you should be up and
running at this point
from here you can actually do some
database thing this like we're your
database and just connecting is nothing
but really this is something that you
could get hung up on so now at this
point you can connect and and from there
you can create some tables and and do
some meaningful things
thanks for watching
Video Length: 06:41
Uploaded By: Ken Swartwout
Published: 12/11/2013
View Count: 1,972,641