Holiday Logo Design Website Header Templates Register domain Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-25-2007, 09:36 AM
Junior Member
 
Join Date: Mar 2007
Posts: 25
Willyke is on a distinguished road
MySQL Tutorial

MySQL is one of the most used database.
With MySQL you don't make websites like some people think but you can save data in it.

Before you can work with a database you need to connect to it and you do it like this:

Code:
<?php
$username= "root"; // Username that has access to your database
$password= ""; //Password that has access to your database
$host = "localhost"; // The host where the database is (mostly this is localhost)
$db = "databasename"; // The name of the database where you want to work with

//Now we going to connect to the database

mysql_connect($host,$username,$password);
mysql_select_db($db);
?>
Now we're going to see the following statements:

- CREATE
- SELECT
- INSERT
- UPDATE
- DROP
- ALTER
- DELETE

CREATE:
Before you can work with a table in a certain database you need to create it with the statement CREATE. You do that like this:


Code:
CREATE TABLE tablename(
fieldname datatype,
fieldname datatype,
);

These are the different datatypes you can choose from:

Datatype / Explanation

TINYINT Integer from -128 tot 127
SMALLINT Integer from -32.768 tot 32.767
MEDIUMINT Integer from -8.388.608 tot 8.388.607
INT Integer from -2.147.483.648 tot 2.147.483.647
BIGINT Integer from -9.223.372.036.854.775.808 till 9.223.372.036.854.775.807
FLOAT number with a ','
DOUBLE number with a ',' but with is more specifier
DATE UNIX-date
DATETIME UNIX-date and time
CHAR String with a number of characters
VARCHAR String with a number of characters
TINYBLOB Little BLOB-field
TINYTEXT Little TEXT-field
TEXT Textfield without restrictions, comes in handy for big documents
BLOB (Binairy Large OBject) The same as TEXT but only with BINARY DATA
LONGBLOB Huge BLOB field
LONGTEXT Huge TEXT field
ENUM No explanation
SET No explanation

Here is an example of a simple table


Code:
CREATE TABLE data(
id int(10) not null auto_increment PRIMARY KEY,
firstname varchar(200) not null,
surname varchar(255) not null,
age varchar(10) not null,
location varchar(255) not null)

But you can also make your table with PHPMyAdmin but that isn't installed on every server so its easy if you can do it without!

Code:
<?php
$username= "root"; // Username that has access to your database
$password= ""; //Password that has access to your database
$host = "localhost"; // The host where the database is (mostly this is localhost)
$db = "databasename"; // The name of the database where you want to work with

//Now we going to connect to the database

mysql_connect($host,$username,$password);
mysql_select_db($db);


$table= "CREATE TABLE data(
id int(10) not null auto_increment PRIMARY KEY,
firstname varchar(200) not null,
surname varchar(255) not null,
age varchar(10) not null,
location varchar(255) not null)";

mysql_query($table) or die(mysql_error());
echo "The table is succesfully added!";
?>
If the table has been added you will see the sentence:
The table is succesfully added!

SELECT:

SELECT is one of the most used statements because you get the data out your database witht this statement

You can do this on different ways


Code:
<?php
SELECT * FROM tablename
// Now you select everything

SELECT sub-table FROM tablename
// now you select sub-table(by example: name) from tablename

SELECT * FROM tablename WHERE firstname='Wesley'
// now you select everything out the table where the first name is Wesley

SELECT sub-table FROM tablename ORDER BY id DESC
// now it wil put the new data at the top

SELECT * FROM tablename ORDER BY firstname ASC
// now he will put the firstnames in alphabetical order

SELECT * FROM tablename WHERE id <= '5'
// Now he selects everything where the id is 5 or smaller
?>
There are much more like that but i think i gave enough examples.

Code:
<?php
//Make a connection with the database
//i don't put it here anymore, you will need to put it here by yourself (its on the top of the topic)

$sql = "SELECT * FROM data";
$query = mysql_query($sql);
//Now we are going to make a loop
while ($show = mysql_fetch_object($query))
{
echo "<b>Firstname: </b> $show->firstname<br>";
echo "<b>Surname: </b> $show->surname<br>";
echo "<b>Age: </b> $show->age<br>";
echo "<b>Location: </b> $show->location<br>";
}
?>
Now it will give the output:
Firstname:
Surname:
Age:
Location:

This is because there isn't anything in the tabel yet.

INSERT:

With the statement INSERT you can save data in your database


Code:
<?php
INSERT INTO tablename(sub-table 1, sub-table 2, sub-table 3, sub-table 4) VALUES  
('this comes in sub-table 1','this comes in sub-table 2','this comes insub-table 3','this comes in sub-table 4')
?>

This was the statement INSERT, now i'll give an example

form.html


Code:
<html>
<head>
<title>Add data into the database</title>
</head>
<body>

<form method="post" action="add.php">
<input type="text" name="firstname">
<input type="text" name="surname">
<input type="text" name="age">
<input type="text" name="location">
<input type="submit" name="submit" values="Add into database">
</form>

</body>
</html>

add.php

Code:
<?php
//Connect to the database first

$insert = "INSERT INTO data (id, firstname, surname, age, location) VALUES
('','$firstname,'$surname,'$age,'$location)";
mysql_query($insert) or die(mysql_error());
echo "The data has been added succesfully!";
?>

UPDATE:

If you want to change something lik your age we use the statement UPDATE.

This is how you do that:

Code:
UPDATE table SET sub-table='new data'
Think about it that you always need to use mysql_query(); to let the query work.


DROP
You need to watch out with this statement because it can delete whole tables.

Code:
DROP DATABASE databasename// Delete a whole Database
DROP TABLE tablename// Delete a database table
DELETE

Code:
DELETE FROM tablename

<?php
//an example
$delete = "DELETE FROM data WHERE firstname= 'Wesley'";  
mysql_query($delete) or die(mysql_error());  
echo "It is succesfully deleted!!";
?>
ALTER
With ALTER you can add/change/delete sub-tables in a table

Code:
<?php
// lets rename the table data to members
ALTER TABLE data RENAME AS members  
// Now we add the column password
ALTER TABLE members ADD COLUMN password varchar(255)  
// now we delete the column  surname
ALTER TABLE members DROP COLUMN surname
?>

Reply With Quote
  #2 (permalink)  
Old 03-29-2007, 05:38 PM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 120
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
Good tutorial, a nice ammount of detail used.

Cheers

Base

http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #3 (permalink)  
Old 03-30-2007, 02:43 PM
Junior Member
 
Join Date: Mar 2007
Posts: 5
sandprince is on a distinguished road
this tutorial is cool , but for what is he...
Reply With Quote
  #4 (permalink)  
Old 03-31-2007, 09:38 AM
Junior Member
 
Join Date: Mar 2007
Posts: 25
Willyke is on a distinguished road
Quote:
Originally Posted by sandprince View Post
this tutorial is cool , but for what is he...
It is a basic php tutorial to learn to work with PHP
Reply With Quote
  #5 (permalink)  
Old 03-31-2007, 05:32 PM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 120
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
Less of a baisic PHP tutorial, than a baisic MySQL & PHP tutorial. :P

Base

http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #6 (permalink)  
Old 04-01-2007, 01:47 PM
Junior Member
 
Join Date: Mar 2007
Posts: 25
Willyke is on a distinguished road
Quote:
Originally Posted by Base View Post
Less of a baisic PHP tutorial, than a baisic MySQL & PHP tutorial. :P

Base
That's what I ment but forgot to type & MySQL at the end of my sentence xD, I was a bit in a hurry, my mom just said I could go and buy me a PS3 ^^
Reply With Quote
  #7 (permalink)  
Old 04-02-2007, 06:39 PM
Junior Member
 
Join Date: Apr 2007
Posts: 13
frozenravage is on a distinguished road
been looking for this kind of tutorial for quite sometime, tnx dude!
Reply With Quote
  #8 (permalink)  
Old 06-25-2007, 11:35 AM
Junior Member
 
Join Date: Jun 2007
Posts: 20
Vendetta is on a distinguished road
Thumbs up

good tutorial
thanks
Reply With Quote
  #9 (permalink)  
Old 07-12-2007, 01:06 AM
Junior Member
 
Join Date: Jul 2007
Posts: 18
Madis is on a distinguished road
I should have visited this place before, it took me a while before I understood what all of them do. Good explanations.
Reply With Quote
  #10 (permalink)  
Old 07-14-2007, 12:20 PM
Junior Member
 
Join Date: Jul 2007
Posts: 4
Fusion123 is on a distinguished road
Thx man i like it

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 03:52 PM.



Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com