Lost Password?


  #1 (permalink)  
Old 01-07-2007, 05:22 PM
Jaan's Avatar   
Jaan Jaan is offline
Mod
 
Join Date: Dec 2006
Location: Estonia
Age: 17
Posts: 930
Last Blog:
AdStar Ad Control Pa...
Rep Power: 16
Jaan is a jewel in the roughJaan is a jewel in the roughJaan is a jewel in the roughJaan is a jewel in the rough
Send a message via MSN to Jaan
Default Simple PHP/MYSQL guide

Well in this tutorial i will show how to do connect to database, how to add things and how to update things in your database.

1. Connecting

Syntax:

Code:
mysql_connect(servername,username,password);
Example:
PHP Code:
<?php

$connect 
mysql_connect("localhost""MyUsername""MyPassword");
// If there's problem with connecting to database then this part will show this problem
if(!$connect){
die(
"Cannot connect to database ".mysql_error());
}

?>
2. Selecting a database

Syntax

Code:
mysql_select_db(database_name);
Example:

PHP Code:
<?php

$connect 
mysql_connect("localhost""MyUsername""MyPassword");
if(!
$connect){
die(
"Cannot connect to database ".mysql_error());
}

mysql_select_db("my_database");

?>
3. Creating a database

Syntax:

Code:
CREATE DATABASE databasename
Example:

PHP Code:
<?php

$connect 
mysql_connect("localhost""MyUsername""MyPassword");
if(!
$connect){
die(
"Cannot connect to database ".mysql_error());
}

if(
mysql_query("CREATE DATABASE my_database"){
echo 
"Database created!";
}else{
echo 
"There was problem with creating a database: ".mysql_error();
}

?>
4. Inserting into database

Syntax:
Code:
INSERT INTO table_name VALUES (first_value, second_value, third_value)

or

INSERT INTO table_name (first_column, second_column, third_column) VALUES (first_value, second_value, third_value)
Example:

PHP Code:
<?php

$connect 
mysql_connect("localhost""MyUsername""MyPassword");
if(!
$connect){
die(
"Cannot connect to database ".mysql_error());
}

mysql_select_db("my_database");

mysql_query("INSERT INTO users (username, email, age) VALUES ('Jaan', 'jaan(at)et-resources.com', '15')");

?>
5. Selecting from database

Syntax:

Code:
SELECT column(s) FROM table_name
Example:


PHP Code:
<?php

$connect 
mysql_connect("localhost""MyUsername""MyPassword");
if(!
$connect){
die(
"Cannot connect to database ".mysql_error());
}

mysql_select_db("my_database");

// This * means ALL so.. select all from users
mysql_query("SELECT * FROM users");

?>
Example 2:

PHP Code:
<?php

$connect 
mysql_connect("localhost""MyUsername""MyPassword");
if(!
$connect){
die(
"Cannot connect to database ".mysql_error());
}

mysql_select_db("my_database");

mysql_query("SELECT email FROM users");

?>
Example 3:

PHP Code:
<?php

$connect 
mysql_connect("localhost""MyUsername""MyPassword");
if(!
$connect){
die(
"Cannot connect to database ".mysql_error());
}

mysql_select_db("my_database");

$select mysql_query("SELECT * FROM users");

// This creates a loop which will repeat itself until there are no more rows to select from the database. We getting the field names and storing them in the $row variable. This makes it easier to echo each field. 
while($row mysql_fetch_array($select)){

echo 
$row['username']. "<br>";
echo 
$row['age'];

}

?>

This is basic what you need to know. If you have more questions feel free to ask from me.

Have fun!
__________________


Cheap & Professional Web Design | Need help? Send a PM
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Project: ionFiles - Joomla Simple File Download Jordan Community Projects 324 11-10-2008 08:34 PM
Project: ionFiles - Joomla Simple File Download - Mirror 2 Jordan ionFiles 6 11-06-2008 01:24 PM
Simple Hex-Editing TcM Tutorials 31 11-01-2008 10:51 AM
Simple gallery Jaan PHP Tutorials 27 05-19-2008 05:56 PM
Beginners Guide To PHP: *Tutorial* renlok PHP Tutorials 3 04-22-2008 04:20 PM


All times are GMT -5. The time now is 07:32 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 97%

Ads