0% found this document useful (0 votes)
48 views

Chap 8

MySQL is a relational database system that allows information to be separated into tables that can be linked together. Each table contains fields that hold different data types like text, numbers, and dates. Fields can be defined as different types and whether they allow null values. Indexes help expedite searches and primary keys must be unique identifiers. PHP functions like mysql_connect(), mysql_query(), and mysql_fetch_array() allow MySQL commands and queries to be run from within PHP code.

Uploaded by

snehal110
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Chap 8

MySQL is a relational database system that allows information to be separated into tables that can be linked together. Each table contains fields that hold different data types like text, numbers, and dates. Fields can be defined as different types and whether they allow null values. Indexes help expedite searches and primary keys must be unique identifiers. PHP functions like mysql_connect(), mysql_query(), and mysql_fetch_array() allow MySQL commands and queries to be run from within PHP code.

Uploaded by

snehal110
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

MySQL Structure and Syntax MySQL is a relational database system, which basically means that it can store bits

of information in separate areas and link those areas together. You can store virtually anything in a database. MySQL commands can be issued through the command prompt or through PHP. Because MySQL is a relational database management system, it allows you to separate information into tables or areas of pertinent information. In non-relational database systems, all the information is stored in one big area, which makes it much more difficult and cumbersome to sort and extract only the data you want. In MySQL, each table consists of separate fields, which represent each bit of information. Fields can hold different types of data, such as text, numbers, dates, and so on. You create database tables based on what type of information you want to store in them. The separate tables of MySQL are then linked together with some common denominator, where the values of the common field are the same. Field Types When you create a table initially, you need to tell MySQL server what types of information will be stored in each field. MySQL Field char(length) varchar(length) Type Description any character can be in this field, but the field will have a fixed length. any character can be in this field, and the data can vary in length from 0 to 255 characters; maximum length of field is denoted in parentheses. numeric field that stores integers that can range from 2147483648 to +2147483647, but can be limited with the length parameter; the length parameter limits the number of digits that can be shown, not the value; mathematical functions can be performed on data in this field. numeric field that stores positive integers (and zero) up to 4294967295; the length parameter limits the number of digits that can be displayed; mathematical functions can be performed on data in this field. any character can be in this field, and the maximum size of the data is 65536 characters. numeric field that can store decimals; the length parameter limits the number of digits that can be displayed, and the dec parameter limits the number of decimal places that can be stored; for example, a price field that would store prices up to 999.99 would be defined as decimal(5,2). allows only certain values to stored in this field, such as true, false, or a list of states. 65535 different options are allowed. stores a date as yyyy-mm-dd. stores time as hh:mm:ss. multipurpose field that stores date and time as yyyy-mm-dd

int(length)

int(length) unsigned

text decimal(length,dec)

enum(opt1, opt2, )

date time datetime

hh:mm:ss. Null/Not null Your MySQL server also wants to know whether or not the field can be empty. You do this with the null or not null option. null tells MySQL that it is okay if nothing is stored in the field, and not null tells MySQL to require something, anything, to be stored there (number zero is different from a null entry). If a field has been defined as not null and nothing is entered by the user, MySQL will enter a 0 in the field instead of producing an error. You should not rely on MySQL to check data for accuracy and instead put checks into place using PHP. Null= Val= 0 Indexes MySQL uses indexes to expedite the process of searching for a particular row of information. By using an internal filing system, MySQL can jump to the approximate location of your data much more quickly. It does this through the use of indexes, also known as keys. MySQL requires at least one index on every table, so that it has something to go by. Normally, you would use a primary key, or unique identifier that helps keep the data separate. This field must be not null and unique; MySQL also provides a feature that allows a value in a field to be automatically incremented by one. This auto_increment parameter is useful for making sure your primary key is being populated with unique numbers. Unique When this parameter is turned on, MySQL makes sure that absolutely no duplicates exist for a particular field. Typically, this is used for only the primary key in your table, but it can be used with any field. Auto Increment You can designate a field to be auto incremented by simply adding the auto_increment command when setting up your table. You can also determine what the first number in the count will be, if you dont want it to be 1. Commands Typically, you keep the MySQL commands in all caps, although this is not necessary. The purpose of this is to help keep the MySQL syntax separate from the variables and table or database names.

CREATE creates new databases and tables ALTER modifies 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 INSERT INTO table_name VALUES - puts values into the table UPDATE - lets you modify data already in a table DROP - deletes an entire table or database PHP and MySQL With the onset of PHP5, you need to take a few extra steps to convince PHP and MySQL to play well with each other. You can use MySQL commands within PHP code almost as seamlessly as you do with HTML. Numerous PHP functions work specifically with MySQL to make your life easier. Some of the more commonly used functions are:

mysql_connect(hostname, user, pass) connects to the MySQL server.

mysql_select_db(database name)

equivalent to the MySQL command USE; makes the selected database the active one
mysql_query(query)

Used to send any type of MySQL command to the server.


mysql_fetch_rows(results variable from query)

Used to return a row of the entire results of a database query.


mysql_fetch_array(results variable from query)

Used to return several rows of the entire results of a database query.


mysql_error() shows the error message that has been returned directly from the MySQL server.

You can also send any MySQL command to the server through PHP and the mysql_query command. You do this by sending the straight text through PHP either through a variable or through the mysql_query command directly: $query = SELECT * from TABLE;

$results = mysql_query($query); or $results = mysql_query(SELECT * from TABLE); The results of your query are then put into a temporary array known as $results.

Connecting to the MySQL Server You must first connect to the MySQL server using your specific connection variables. Connection variables consist of the following parameters: Host name - its the Username and password local host because youve installed everything locally.

You issue this connection command with the PHP function called mysql_connect. As with all of your PHP/MySQL statements, you can either put the information into variables, or leave them as text in your MySQL query. With variables: $host = localhost;

$user = bp5am;

$pass = bp5ampass;

$connect = mysql_connect($host, $user, $pass); or $connect = mysql_connect(localhost, bp5am, bp5ampass); Querying the Database If you want to retrieve the database you have to use the SELECT statement to choose data that fits your criteria, like:

SELECT [fieldnames] AS [alias] FROM [tablename] WHERE [criteria] ORDER BY [fieldname to sort on][DESC] LIMIT [offset, maxrows] You can set numerous other parameters, but these are the most commonly used: SELECT [fieldnames] first decide what specific fieldnames you want to retrieve; if you want to see them all, you simply insert *. AS use to group two or more fieldnames together so that you can reference them later as one giant variable. SELECT first_name, last_name AS full_name. . . ORDER BY full_name . . . FROM you just need to name the table or tables you are pulling the data from. WHERE list your criteria for filtering out the data ORDER BY use this if you want the data sorted on a particular field; in descending order, add DESC. LIMIT to limit the number of results returned and offset the first record returned to whatever number you choose. WHERE While SELECT tells MySQL which fields you want to see, WHERE tells it which records you want to see. SELECT * FROM customers

//retrieves all information about all customers

SELECT * FROM customers WHERE gender = Male

//retrieves all information about male customers Lets look at the WHERE clause a little more in-depth: Tables You can get information from more than one table in two ways: - reference the individual tables in your query and link them temporarily through a common field formally JOIN the individual tables in your query 1. Referencing Two Tables You can distinguish between two tables in your database by referencing them in the SELECT statement: $query = SELECT customers.name, orders.order_total FROM customers, orders WHERE customers.cust_ID = orders.cust_ID;

//retrieves customers names from customers table and order_total from //orders table where the cust_ID field in the customers table equals the //cust_ID field in the orders table. Although you are linking the two tables through the cust_ID field, the names do not have to be the same. You can compare any two field names from any two tables. $query = SELECT customers.name, orders.order_total FROM customers, orders WHERE customers.email = orders.shiptoemail; //retrieves customers names from customers table and order_total from //orders table where the email field in the customers table equals the //shiptoemail field in the orders table. This would link your tables through the email and shiptoemail fields from different tables. 2. Joining Two Tables The JOIN function gives you greater control over how your database tables relate to and connect with each other, but it also requires a greater understanding of relational databases $query = SELECT movie_name, movietype_label . LEFT JOIN movietype . ON movie_type = movietype_id . ORDER BY movie_type;

You might also like