Well, I though that maybe I could help some really noob (since I’m a noob too) people about how to connect to a database, and extract some stuff out of it. And explaining it so even someone who doesn't code would understand...(is that possible ?).
ps : don't lecture me on my typo :/, and I'll be talking about mysql (most popular db)
First off, you need to ask yourself some questions.
Can you have access to a server that runs php and mysql?
Do you need a database ?
Can you do it yourself ?
Well here's the answer boys and girls
I have no idea, but I can tell you one thing, I love meat. hell ya, meat is soooo good.
Well this one is a tricky one, imo (in my opinion) as soon as you have to keep track of some data (data is pretty vague.. so let's say it could be keeping track of all your employees in/out or even how many cows you bought this year). Then you could use a database.
Get it ? another exemple, your a hot looking guy!! it's a nice start, but why would you need a database, I mean your already so hot!! Well I’ll tell you, let's say you go out often and you meet so many chicks you can't really keep track anymore, you lost countless of numbers because a piece of paper just isn't very reliable for more then 2 days and everyone knows, you wait at least 4-5 days before you call back.
Now with a database, you could simply take that piece of paper enter it into a digital phone book before you lose it! Imagine, no more deception no more " Awwww I can't believe I forgot her number she was SO HOT". No my friend, database is really the answer you are looking for.
Yes, but you have to feel the database, you have to think like a database, you have to know what a database dreams of. You have to ask yourself every minute, if I was a database would I be doing that? Database coding is more then just programming, it's a way of living, are you up to it ?
Let's move on. I shall add that if at anytime you feel like you need to go to the bathroom you can just do so, I won't be mad if you just interrupt me get up and go pee. Okay now I know you want to get a leak, go ahead. I'll wait.................................................
Ready to continue ? Okay to the good stuff now.
today are mainly relational database. Just so you know, won't change a thing for you, because your a noob if you reading this, but it's always nice to plug that in a conversation, so you *look* intelligent.
Okay, we have the word relation. Well hey you probably guest it, there's relations between *stuff* in a database.
And we are going to define *stuff*
Stuff: let's call them Tables
Okay we are making progress people.
So what's a table
basically a table is an entity that's made of rows and columns.
Let’s see what it's like here
See there's a bunch of fields names
) and you have data
entered ( actually only have 1 row
Let's extrapolate here, I wanted to keep track of a user information.
Well okay I have a first name last name address etc. Now there's 1 thing you should actually find odd, and you could find 2 things odd if you’re like, awesome, or demi-god.
First thing is, I have a field name userid. What in god's name is that field for ? I'll explain soon.
Second thing is you can actually see my password!!! Yes, well, that's wrong a password should always be "encrypted". But I'm a noob so I’m happy with my non-secure database. Heck it's still a database right
and there's a bonus one. You didn't see my TABLE NAME, yes table HAVE names. Now you know. My table name is users.
Okay so about my field userid. I'll tell you a secret, I CREATED this field from scratch. WHY!!??? You must say.
Because EVERY table MUST have a primary key
What is a primary key
A primary key value identifies A SPECIFIC RECORD throughout the entire database.
A primary key field identify a given TABLE throughout the entire database.
A primary key must ALWAYS be unique.(because then it couldn't identify a specific record throughout the database!)
A primary key may be composed of more then one field, it's called a composite key.
Okay that was confusing. Let s go back to my table ( the image)
Let's forget about userid for a sec. Okay could First name identify a specific record throughout the database ?
it's no... Of course not! think of it, I have kader as a name, if it was the primary key, well kader could identify the whole row. With kader I could extract the last name of kader's address!. Yes BUT, not really..hehe let me tell you why!
If I have a second user that names is kader, YOU'R in deep sh... yeah you got it.
So let's go on, what about Last name ?
No, same as Fname.
Address ? no, City ? no, Country ? HELL no.
Email ? Well what if you share your Internet provider’s email with your family ? Again, no.
first name AND last name ? Well yeah there's a slim chance of someone having the same fname and lname. BUT there is STILL a chance.
And a rule of thumb, always take the simpler way (which is a primary key made of one field).
Okay so come back to my userid I CREATED. (yeah I’m a real genius)
inserted there as a value.
So basically what userid does, it just a field that is set to AUTO INCREMENT. What it means, is now you see 1. Well if you add another user's information, userid
will AUTOMATICALLY insert 2 for the new user (it will be a second row of information) wow... it's magic up to now, scientifics all around the world can't explain how it auto increment itself.. IT'S magic I tell you.
Imagine it, userid perfectly fits the primary key need. Never duplicate (the number always change ..it adds 1 at every inserted data). and it will exactly define a specific row!
Where userid = 1 first name is ABSOLUTLY only equal to Kader there's no other possibility (see how useful PK are!!) pk = primary key btw.
Now we got our PK.
what else are they good for. Pk is like superman, he (the PK
) can do MUCH MORE then what I just explained. They are used to actually LINK 2 tables together. YES you heard me, you can have more then one table, heck you can even get 33 tables if you wanted.
So let's invent something. Let's say you are the father of 2 sons, one of them is that previously hot guy we talked about. Second one is as good looking! You’re a businessman too. What you'll do ? Well you start making money off your son’s hard labour!!! You make a loaning system! When a *client* need to be cheers up one of your 2 sons go help them out. BUT HEY you need to know which son visited what client ?
Okay so you need a database for that!.
As you can see there's a FOREIGN KEY?
Is it like your foreign neighbour that came in a boat from another continent ?? Well a little bit.
First off a foreign key is actually a primary key from another table!
loanerid is a primary key of table LOANERS BUT it is a foreign key of table CLIENTS. Get it it's almost the same as your foreign neighbour( even I fail to find a link.. ).
So basically with a bit of code we could just ask
WHERE clientid = 2 (that's our friend Ronald McDonald)
you could view all the fields WHERE clientid =2. So you would see loanerid 1.. Well with some more code you can join the information from table loaner like this
Tired? It's okay I’ll get you part II soon where you could see actual SCRIPT
EDIT : I actually corrected most of typos