PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Nov 24, 2014 1:39 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: Wed Jul 28, 2004 12:26 pm 
Offline
Forum Regular

Joined: Fri Aug 15, 2003 12:25 pm
Posts: 577
Location: Montreal, Canada
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.

1- Can you have access to a server that runs php and mysql?
2- Do you need a database ?
3- Can you do it yourself ?

Well here's the answer boys and girls

1- I have no idea, but I can tell you one thing, I love meat. hell ya, meat is soooo good.

2- 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.

3- 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.

Databases 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 (each columns) 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 ?
Yes.........???
WRONG 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)
There’s 1 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!.
here another image.
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

Enjoy

EDIT : I actually corrected most of typos 8)


Last edited by Draco_03 on Thu Jul 29, 2004 8:35 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 6:34 pm 
Offline
DevNet Master
User avatar

Joined: Thu Aug 15, 2002 5:53 am
Posts: 4235
Location: Sussex, UK
Nice to see this tutorial. A couple of suggestions, if you don't mind:
1. McGruff's Starter Pack: http://forums.devnetwork.net/viewtopic.php?t=21400 which contains a link to: http://www.oreilly.com/catalog/javadtab ... r/ch02.pdf

2. We're putting most tutorials in the Wiki now

3. Please: before posting, run something you want to be a resource to others through a spellchecker. You owe that to the standard you want to set. If you want me to lecture you about typos: I will :twisted:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 29, 2004 8:39 am 
Offline
Forum Regular

Joined: Fri Aug 15, 2003 12:25 pm
Posts: 577
Location: Montreal, Canada
Thx for the input
3- yes.. you are right.... (but I just....I couldn't...I...have no real good reasons, but just my lazyness, and the fact english is not my first language)

8)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 29, 2004 2:56 pm 
Offline
DevNet Master
User avatar

Joined: Thu Aug 15, 2002 5:53 am
Posts: 4235
Location: Sussex, UK
English isn't my first language either, but I'm trying to make an effort. Why not run that tutorial of yours past a spellchecker - every major office wordprocessor has one?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 29, 2004 2:57 pm 
Offline
Forum Regular

Joined: Fri Aug 15, 2003 12:25 pm
Posts: 577
Location: Montreal, Canada
That's what i did :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:53 am 
Offline
DevNet Master
User avatar

Joined: Thu Aug 15, 2002 5:53 am
Posts: 4235
Location: Sussex, UK
Great. Why not move it over to the tutorial section in the Wiki, that's where we're moving tutorials one by one. That way, everyone can add to it and it stays up to date.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 01, 2004 10:21 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Good tutorial, I would keep away from all the useless comments you make though... got rather annoiying after a bit ;)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 8:30 am 
Offline
Forum Regular

Joined: Fri Aug 15, 2003 12:25 pm
Posts: 577
Location: Montreal, Canada
What useless comment!!! 8)
hehe okay next time I'll put less stoopid comments..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 9:21 am 
Offline
DevNet Resident
User avatar

Joined: Fri Aug 08, 2003 6:53 pm
Posts: 2100
Location: Sweden
Some interesting thoughts about useless comments:
(And have in mind, that this is just what I wrote; thoughts... ;))

The creator (for example Draco_03) of a tutorial could/should create a new thread in the discussed part of the forum (Databases), linking to it with the description "Discussion about the tutorial here". This as the Wiki isn't very wellsuited for discussions. It's not in the Wiki atm, but it likely will be (or rather, should?).

Also do send PM's to the author if something needs to be changed (spelling, errors(!) and such) instead of pointing it out in the thread. This will make the looks of the board abit more readable and the searchengine would get more correct hits when being used.

Good work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 4:50 pm 
Offline
Forum Newbie

Joined: Tue Aug 10, 2004 4:50 pm
Posts: 10
Location: Grand Prarie, TX
Phenom wrote:
Good tutorial, I would keep away from all the useless comments you make though... got rather annoiying after a bit ;)


The useless comments might get to you, but at least this person is trying to help someone by posting a tutorial.. :o
And trying to help someone..!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 9:08 pm 
Offline
Moderator
User avatar

Joined: Sat Apr 20, 2002 3:14 pm
Posts: 1416
Location: Dallas, TX
tazdog wrote:
Phenom wrote:
Good tutorial, I would keep away from all the useless comments you make though... got rather annoiying after a bit ;)


The useless comments might get to you, but at least this person is trying to help someone by posting a tutorial.. :o
And trying to help someone..!


Exactly. Any help is good. :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 10:25 am 
Offline
Forum Regular

Joined: Fri Aug 15, 2003 12:25 pm
Posts: 577
Location: Montreal, Canada
Thx for your input :D
really feel warm inside..
hehe


Top
 Profile  
 
PostPosted: Wed Aug 15, 2012 4:44 am 
Offline
Forum Newbie

Joined: Wed Aug 15, 2012 3:16 am
Posts: 2
good tutorial indeed, but i have a problem dealing with related database tables in mysql when i want to insert data, i usually get an error like this "Cannot add or update a child row: a foreign key constraint fails (`"db_name"`.`test`, CONSTRAINT `test_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`category_id`) ON DELETE CASCADE ON UPDATE CASCADE)" even if i try to change the options on the relationships, i will be glad if you assist me in this. kind regards,


Top
 Profile  
 
PostPosted: Thu Jul 04, 2013 5:16 am 
Offline
Forum Newbie

Joined: Thu Jul 04, 2013 5:10 am
Posts: 2
Good tutorial! I simply want to type a comment to be able to appreciate you for your post, for it may help me a lot.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group