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.
Edited by Eck, 09 January 2006 - 08:56 PM.