MySQL Syntax and Commands
Command | Description |
CREATE | Creates new database or table. |
ALTER | Modify existing tables. |
SELECT | Chooses the data you want. |
DELETE | Erases the data from your table. |
DESCRIBE | Lets you know the structure and specifics of the table. |
INSET INTO tablename VALUES | Puts values into the table. |
UPDATE | Lets you modify data already in the table. |
DROP | Deletes an entire table or database. |
More Commonly Used Functions
Functions | Description |
mysql_connect ("hostname", "user", "password") | Connects to the MySQL Server. |
mysql_select_db ("database name") | Equivalent to the MySQL command USE; make the selected database the active one. |
mysql_query ("query") | Used to send any type of MySQL command to the server. |
mysql_fetch_rows ("results variables from query") | Used to return a row of the entire results of a database query. |
mysql_fetch_array ("results variables from query") | Used to return several rows of the entire results of a database query. |
mysql_error() | Show the error message that has been returned directly from the MySQL server. |
Connecting to the MySQL Server
Example,
$host = "localhost";
$user = "sroyit";
$pass = "sroyitpass";
$connect = mysql_connect ($host, $user, $pass);
or
$connect = mysql_connect ("localhost", "sroyit", "sroyitpass");
Creating a Database and Table
Example:
$connect = mysql_connect ("localhost", "sroyit", "sroyitpass") or die ("Deny, check your server connection!");
$create = mysql_query ("CREATE DATABASE IF NOT EXISTS movie_site") or die(mysql_error());
mysql_select_db ("movei_site");
$movie_create = " CREATE TABLE movie (
movie_id int(11) NOT NULL auto_increment,
movie_name varchar(255) NOT NULL,
movie_type tinyint(2) NOT NULL default 0,
movie_year int(4) NOT NULL default 0,
movie_leadactor int(11) NOT NULL default 0,
movie_director int(11) NOT NULL default 0,
PRIMARY KEY (movie_id),
KEY movie_type (movie_type, movie_year) ) ";
$result = mysql_query ($movie_create) or die (mysql_error());
?>
Inserting Rows in a Created Table
Example:
$connect = mysql_connect ("localhost", "sroyit", "sroyitpass") or dia ("Deny, check your server connection!");
mysql_select_db ("movie_site");
$movie_insert = "INSERT INTO movie (movie_id, movie_name, movie_type,
movie_year, movie_leadactor, movie_director)
VALUES (1,'Sumon',5,2003,1,2),
(2,'Matrix',5,2003,1,2),
(3,'Patriot',5,2003,1,2),
(4,'Tarminator',5,2003,1,2),
(5,'Titanic',5,2003,1,2),
(7,'After the sunset',5,2003,1,2)
";
$result = mysql_query ($movie_insert) or die (mysql_error());
?>