Jump to content


Photo
* * * * * 1 votes

Simple mysql + php tutorial


  • Please log in to reply
6 replies to this topic

#1 Eck

Eck

    Young Padawan

  • Members
  • Pip
  • 14 posts

Posted 09 January 2006 - 08:54 PM

In this tutorial i will teach you some basics of integrating mysql in a PHP document.
Some key functions we will go over are:
mysql_connect();
mysql_select_db();
mysql_query();
mysql_num_rows();
mysql_fetch_array();

These are very handy when creating a php + mysql based website.

mysql_connect();
This function is used for connecting to a mysql sever, so you can go on and select a database from that server to get info from etc...

An example of this is:
<?php
$connect = mysql_connect("localhost","username","password");
?>
The first part(where it says localhost) is the host the mysql server is located on.
The second part, is the name of the user that you are using to connect to the server.
And the last part is the password for the user.

A simple way to test if the connection works is as follows:
<?php
$connect = mysql_connect("localhost","username","password") or die("Cannot Connect To Database");
echo "Connected to mysql.";
?>
That will say: "Cannot connect to database", if the connection attempt does not work, but if it does, it will simply say:
Connected to mysql.

Once you have your connection to mysql open, its time to select a database to use:
mysql_select_db();
This function will select a database from the mysql server that you are connected to.

An example is:
<?php
mysql_select_db("database",$connect);
?>
This will connect to the database: 'database' using the connection: '$connect'.

Now getting that down, we can start on our query's.
These are the main things you must know how to do when making a database driven website.

I will teach you the four main mysql query's:
SELECT - select data from a database
INSERT - insert new data into the database
UPDATE - update old data in a database
DELETE - delete data from a database

First lets talk about how to perform a query.
You must use the following function:
mysql_query();

An example of using this is:
<?php
$query = mysql_query("SELECT * FROM table ORDER BY id ASC");
?>
This may look crazy and confusing, but deep down its actually very very simple.
Lets break it down shall we..
The first part: SELECT * FROM table
means to: select all from the table(A table, if you dont already know, is made up of rows and columns to orgainize data.)

The second part: ORDER BY field ASC
this will order the data by a certain field in the table in ascending order.

Ok now onto the three other query's.
INSERT
<?php
$query = mysql_query("INSERT INTO table (field,field,field) VALUES ('data','data','date')");
?>

INSERT INTO table will insert the following data into the given table.

The (field1,field2,field3) VALUES ('data1','data2','data3') is saying where to put the certain data.
So its telling it to put 'data1', into 'field1', etc..

UPDATE
<?php
$query = mysql_query("UPDATE table SET field1='data1', field2='data2' WHERE field='data'");
?>

UPDATE table SET says to update the given table and to set the following information.

field1='data1', field2='data2' says to update field1 to 'data1' and field2 to data2.

Then the WHERE field='data' part is telling the query to only update the table where a certain field equals data.
So if you have a field labeled 'id' and you only want to update a the fields that belong to a row that has the id of 2.
Then you would use: WHERE id='2'

DELETE
<?php
$query = mysql_query("DELETE FROM table WHERE field='data'");
?>

DELETE FROM table WHERE field='data'
This is simply saying:
delete from the given table where the field 'field' equals data.
So yet again, if you have a field named 'id' and you want to delete all that equal 2.
Just use: DELETE FROM table WHERE id='2'


Well thats it, I'm sorry i didnt explain everything very good, and if you have any comments / suggestions you know what to do. :D

Edited by Eck, 09 January 2006 - 08:56 PM.


#2 meadow

meadow

    Young Padawan

  • Members
  • Pip
  • 224 posts
  • Location:Devon, England
  • Interests:Php, Hockey, mysql, web design.

Posted 14 January 2006 - 07:09 AM

Nice, thankyou very much, hadn't understood some of those functions before.

#3 _*Raremandan_*

_*Raremandan_*
  • Guests

Posted 15 January 2006 - 12:48 PM

Hey,

Not bad mate, already new it but pretty useful for the newbies.

- Dan

#4 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 19 January 2006 - 03:18 AM

Quite simple and basic, yet still useful. Thanks for sharing, mate!

#5 Paint

Paint

    Young Padawan

  • Members
  • Pip
  • 88 posts

Posted 15 March 2006 - 09:02 AM

I am new to PHP and MYSQL thanks for helping me!

#6 BigDog

BigDog

    Young Padawan

  • Members
  • Pip
  • 277 posts
  • Gender:Male
  • Location:Orange County, California
  • Interests:Running, building computers, PC games and BMX and programming.

Posted 15 April 2006 - 02:24 PM

This is a very good tutorial for people starting out!

Thanks!

#7 ICT Helpers

ICT Helpers

    Young Padawan

  • Members
  • Pip
  • 56 posts
  • Location:England

Posted 16 April 2006 - 07:31 AM

I am sure that will help many learning PHP and MySQL :P




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users