This tutorial is about MySql. MySql is a powerful database language (something like a storage thing) that needs moderate Php to be used.
NOTE: if you dont know Php, put a good comment about this post and dont pass the line
------------------------------------------
------------------------------------------
------------------------------------------
--
Now, lets get started:
Introduction
Mysql is a powerful database language, thet you can download it from here. If you are not sure if you/your server have/has it or not, then just write
<?php
phpinfo() ;
?>
and if exists a table titled MySql, then you/your server have/has it!
One VERYpowerful tool you can use to easily manage MySql is phpMyAdmin (I'm sorry, I dont know where to download it, but if you have a browser-based php-server probably it works). With this tool, you can do lot of things with MySql only will be very difficult.
Now, a simple question: What is a database?
A database is a conjunction of tables. A good example will be like having a forum, where the tables could be:
Users
|-----------------------------------------
---------------------------------|
| Username | Password | Number of posts|
|-----------------------------------------
---------------------------------|
|windymager | men00b345 | 139 |
|-----------------------------------------
---------------------------------|
| afro_ninja | MePwNsAlL | 2487 |
|-----------------------------------------
---------------------------------|
Posts
|-----------------------------------------
------------------------------------------
---------|
| Name | Creator | Text |
|-----------------------------------------
------------------------------------------
---------|
|5000 users were banned | WadeFulp | I didnt liked their face|
|-----------------------------------------
------------------------------------------
---------|
| Php: using mysql | windymager(lol) |blablablablablablabla|
|-----------------------------------------
------------------------------------------
---------|
...And you can take and retreive the data from there. (Note: the password thing isnt true)
Connecting to MySql:
Just put some code like:
<?php
//set variables
$dir = "mydir" ;
$username = "windymager" ;
$password = "lol" ;
//connect to mysql
$worked = mysql_connect ($dir, $username, $password) ;
//Check if mysql_connect returned zero, that means that has done an error
if (!$worked)
{
//mysql_error fucntion says the last mysql error
die ("Could not connect to MySql cause: " . mysql_error()) ;
}
echo "You are now connected to mysql!" ;
?>
Note: the direction, username, and password of mysql are generally given by your host
Creating a database:
NOTE: generally, hosts dont let you ceating a database this way. Theres hight probabilty you cannot do this, if that happens, just use the one your serer provided to you
<?php
$worked = mysql_connect("host","username","password"
);
if (!$worked)
{
die('Could not connect cause: ' . mysql_error());
}
//till here, I hope you know from the last part
$name = "database" ;
$sql = "CREATE DATABASE $name";
//mysql_query command executes the command on the string in mysql languaje, in this case is "CREATE DATABASE" command, witch created a database
$result = mysql_query ($sql) ;
//and returns zero on error
if ($result)
{
echo "Database my_db created";
}
else
{
echo "Error creating database: " . mysql_error();
}
?>
Creating a table:
NOTE: if you have PhpMyAdmin, is very convenient to use that program instead of executing code
To create a table, you must use the CREATE TABLE command inside the mysql_query, with this sintaxis:
CREATE TABLE name
{
column_name1 type extra,
column_name2 type extra,
...
last_column_name type extra
}
types:
INT (number)
BIGINT (long number)
DOUBLE (decimal number)
VARCHAR(size) (string)
TEXT (string with 65536 chars max)
LONGTEXT (string with 4294967295 chars max)
DATE (holds date in format (yyyy-mm-dd))
TIME (holds time in format (hh:mm:ss))
There are more types, but sincerely, forget them, only use these.
extras:
NOT_NULL (if this field has not a value, it will be 0 for ints and "" for strings instead on NULL)
AUTO_INCREMENT (the variable increments each time a row is created)
Also there are more, but these are more important.
Example:
<?php
$con = mysql_connect ("host", "username", "password") ;
//mysql_select_db command must be used to use and create tables
mysql_select_db ("db_name", $con) ;
//create the table
$sql = "CREATE TABLE users
{
username VARCHAR(15) ;
password VARCHAR(15) ;
post_number INT NOT_NULL ;
}"
mysql_query ($sql) ;
?>
...And thats it. Now, you will have a table like this
users
|-----------------------------------------
----------------------------|
| Username | Password | post_number|
|-----------------------------------------
----------------------------|
REST OF THE GUIDE WILL BE PUTTED ON A BIT (I WAS OUT OF SPACE) SO JUST WAIT)