Unit - IV (Database Handling in PHP& Mysql)

Download as pdf
Download as pdf
You are on page 1of 6
CHAPTER 9Q DATABASE HANDLING USING PHP WITH MySQL — pate Handling Using PHP with MySQL: Introduction to MySQL: Database terms, Data S. eee: | According to MySQL’s Web site, www mysql.com, MySQL is the world’s most popular opensource database application and is widely used with PHP. MySQL software includes the database server (which keeps the actual data), various client apps (for interacting with the database server) and a number of uilities. In this chapter, you'll leam how to create a simple table by utilizing MySQL’s permitted data types and other attributes. Then you'll learn how to use two separate client apps to interface with the MySQL server, [92 NAMING DaTitbasE COMPONENTS NENTS. ig with databases, you must first determine your requirements. The application’s (or Web site's) objective dictates how the database should be designed. Keeping this in mind, the examples in this and the following chapters will make use of a database that maintains Some user registration information. When constructing databases and tables, use names (technically known as identifiers) that are clear, meaningful, and easy to type. > Identifiers should also comprise only letters, integers, andthe underscore (no spaces). ® Should not be the same as a pre-existing keyword (such as a SQL term or function name) n should be considered case-sensitive, > Cannot exceed 64 characters (roughly). It must be distinctive within its field. > This last rule states that a table cannot have two columns with the same name, nor can a database have two tables with the same name. Vv {tables in the same ¢ wo distinc the syntax for d you prevent difficult ame int However, you can use the same column nally fact, you should). These restrictions can be + will help it wil Following these guidelines is a sensible restriction tha z= [93 DATABASE TERMS To Name a Database’s Elements: i base: 1. Determine the Name of the Datal «rage Simply ensure that the databag This is the simplest and, perhaps, least significant PAT Web host will most like) a is unique to that MySQL server. If you use a hos! pathereve count or domain name “ly Of you with a database name that may or may not include Your SF : Because the information and processes might @PP! initial example will be called site name. 2. Determine the Names of the Tables: The table names only need to be unique within table in this example, which records user registration 3. Choose the Column Names for Each Table. Columns in the user's table will record a user ID, a e password, and the registration date. Table 4.1 displays these columns with sample data appropriate identification. Because MySQL offers a password function, I modified the name of, field to simply pass. This isn't exactly necessary, but it's a great concept. Table No. 4.1: Users Table : TAZA LS Column Name Example | gy user_id 834. - first_name ‘Bary 0110) eit baw 2a ct eaifae TOMaAROTAT A 1 ly to any’ generic site, the database j, re first name, a last name, an email do last_name Database Handling using PHP wi After you’ ve identified establish the data type for each column will contain when you Lusing PHP with yp soosse Handi p evel a 153. Almost every database appr _——_—____ aaa ation falls into Retreat (aloo “into one of three categories: known as Strings) > Numeric > Time and date You can utilize severa} Variations ji rhe precise Selection of cotymn son’ inside MN type sv, but it also influences the gy, ann NOt only determines what information may be stored and wgthe MySQL kinds, how much space tormanee of the database. Table 4.2 includes the majority senoted that some of the one Y ‘ake up, and brief descriptions of each type. It should ‘Se res iT “strictions may vary between MySQL versions. Many of the types have an optional Lenj some of which are MySQL-specific. ets, [], denote an th feature, which limits their size. (The square | a ar tisks dar araineter that should be enclosed in parentheses:) You should limit Ber csracter string into a CHA 9 ach column for performance reasons. However, inserting, fa wb first wo haat at ie Column will result in truncation of the final three characters r 1 st ; a Or, wih a size (CHAR, VARCHAR ae the rest will be lost forever). This is true for any field » and so on). As a result, your length should always to th i sorrespond' to the greatest feasible value (as a number) or string (as text) that can be saved. —___ Table No. 42: MySQL. Data Types Type Size Description CHAR [Length] Length bytes A fixed-length field from 0 to 255 | characters long, | VARCHAR [Length] String length+1or2 [A variable-length field from 0 to | bytes 65,535 characters long. | TINY TEXT String length + 1 bytes | A string witha maximum length of 255 characters. TEXT String length + 2 bytes | A string with a maximum length of | 65,535 characters. MEDIUMTEXT String length + 3 bytes | A string with a maximum length of 16,777,215 characters. on LONGTEXT String length + 4 bytes | A string with a maximum length of 4,294,967,295 characters TINYINT [Length] I byte Range of -128 to 127 or 0 to 255 unsigned. SMALLINT [Length] 2 bytes Range of -32,768 to 32,767 or 0 to ___ | 65,535 unsigned. MEDIUMINT [Ler 3 bytes Range of -8,388,608 to 8,388,607 or 0 De to 16,777,215 unsigned. INT ‘4 byles Range of -2,147,483 to 2,147,483,647 tics) or 0 to 4,294,967,295. ee % Description 1s4 -_ PUP 4 | f -9,223,372,036,854 Type = | Range ot 936,854 s 9,223, ne rd ve Yi 46,744,07 3,709,551,61 unsigned ‘A. small number with float FLOAT [Length, | 4 bytes | decimal point Decimals] | ‘Atarge number with a floating decimg DOUBLE [Length, 8 bytes point. Decimals) ‘A Double stored as a string, allowing DECIMAL [Length, | Length + Lor 2byt®S | © 4 fixed decimal point _ Decimals] ___——T7 the format of YYYY-MM-DD E by ahh bo cm f | DATE el __3byKS the format of YYYY-MM.pp DATETIME | 8 bytes HH:MM:SS___ a i Tq the format of YYYYMMpp) TIMESTAMP 4 bytes ISS; acceptable range start i, 1970 and ends in the year 2038. ae ee Te Tn the format of HH:MM:SS. EN ieeatye Short for enumeration, which mean that each column can have one of, several possible values. | SET To 5d or 8 bytes || Bile NUR extent. tat cach cole we can have more than one of sever) possible values. | 9.4.1 To Choose the Column Types: 1. Determine if a column should be of the text, number, or date/time format (Table 4.3); Normally, this is a simple and straightforward process, but you want to be as specifics possible. The date 2006-08-02 (MySQL format), for example, could be kept as a string-August 2, 2006. However, if you use the correct date format, you'll have a more useful database’ (and, as youll see, there are functions that can conyert 2006-08-02 to August 2, 2006). Table No. 4.3: Users Table Column Name Type user_id Number first_name Text last_name Text email Text pass: Text Fesistrationldats Date/Time Database Handling using PHP wir, aS 2. Sel ee umn (Table 4.4): ct the best subtype for each colt The user_id is set ag million values (as an 1a MEDIUMINT in this example, allowing for up to approximately 17 DATETIME. ttmay a. 8°40 non-negative munber) The Tegistration_date is going to be a ss only the dat ae date as well as the exact time a user registered. Consider if you'll want : © date, the time, or mayb ay side of storing too much data Pte to na eke sal Senerally be VARCHAR because their lengths will vary from record to recor he Ption is the password column, which will be « fixed-length CHAR (you'll \ discover why in the next chapter when entering entries). More information on these ‘wo kinds can be found in the "CHAR vs. VARCHAR" sidebar. | Table No. 4.4: Users Table ; ___ Column Name Type ' user_id MEDIUMINT first_name VARCHAR last_name VARCHAR email VARCHAR pass CHAR 7 Tegistration_date DATE TIME "3, Define the maximum text column length (Table 4.5): » Any ficld’s size should be limited to the smallest possible value based on the largest possible Fioput. For instance, if column has a state abbreviation, itis defined as a CHAR(2). Other times, you | may have to gues: I can’t think of any first names longer than roughly 10 characters, but I’ll allow "for up to 20 just to be safe. Table 4.5: Users Table Column Name Type user_id MEDIUMINT | first_name VARCHAR (20) | ! last_name VARCHAR (40) email VARCHAR (60) pass CHAR (40) registration_date DATE TIME oo i a |ClCU PHP & 4 = 18g 186 ‘olumn Properties: “ee onal Col ves wo employ for Your ¢ plumns, YOu shoyy 9.4.2 Choosing Ad . si Aside from determining the data types and consider a few other properties. Nt das NOT NULL. In dataty To begin with, any column, regardless of tyPe, CAR hed aa value — and programming, the NULL value means that the field has peeiereiet table in a correctly const ASE shouly Ideally, every column of every row in every ery vada the NOT NULL description tothe col 1 case. have a value, however this isn’t always the for example, can be ex; a ‘Press type to force a field to have a value. A required mon s as cost DECIMAL (5,2) NOT NULL. etary amount, yy column, regardless of tyPe> when creating a table, jy ds will have the same value for a column, Choosing » serting new rows (unless that row’s valye You can also give a default value for an; circumstances when the majority of the reco v default saves you from having to specify a value when in: for that column differs from the norm). ENUM(‘M’, ‘F’) gender default ‘F” If no value is Supplied for the gender field when adding a record, the default will be used. When the number types are marked as UNSIGNED, the recorded data is limited to positive values and zero. This practically increases the range of positive integers that can be stored (sin no negative values are preserved, as shown in Table 4.2). You can additionally mark the number types as ZEROFILL, which indicates that any exty space is immediately padded with zeros (ZEROFILLs are also automatically UNSIGNED). 9.5 MODEL QUESTIONS i i ei : ‘Answer the following questions. 6 or 8 marks 1. What are some common terms used in databases and what do they mean? 2. Describe MySQL Datatypes. ‘i kt

You might also like