Procedural Style
My Sql Data types +
In MySQL there are three main types : text, number, and Date/Time types.
Text types:
Data type Description ----------- ------------- CHAR(size) Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis. Can store up to 255 characters VARCHAR(size) Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis. Can store up to 255 characters. Note: If you put a greater value than 255 it will be converted to a TEXT type TINYTEXT Holds a string with a maximum length of 255 characters TEXT Holds a string with a maximum length of 65,535 characters BLOB For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters MEDIUMBLOB For BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data LONGTEXT Holds a string with a maximum length of 4,294,967,295 characters LONGBLOB For BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of data ENUM(x,y,z,etc.) Let you enter a list of possible values. You can list up to 65535 values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted. Note: The values are sorted in the order you enter them. You enter the possible values in this format: ENUM('X','Y','Z') SET Similar to ENUM except that SET may contain up to 64 list items and can store more than one choice Number types: Data type Description -------- ------------ TINYINT(size) -128 to 127 normal. 0 to 255 UNSIGNED*. The maximum number of digits may be specified in parenthesis SMALLINT(size) -32768 to 32767 normal. 0 to 65535 UNSIGNED*. The maximum number of digits may be specified in parenthesis MEDIUMINT(size) -8388608 to 8388607 normal. 0 to 16777215 UNSIGNED*. The maximum number of digits may be specified in parenthesis INT(size) -2147483648 to 2147483647 normal. 0 to 4294967295 UNSIGNED*. The maximum number of digits may be specified in parenthesis BIGINT(size) -9223372036854775808 to 9223372036854775807 normal. 0 to 18446744073709551615 UNSIGNED*. The maximum number of digits may be specified in parenthesis FLOAT(size,d) A small number with a floating decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter DOUBLE(size,d) A large number with a floating decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter DECIMAL(size,d) A DOUBLE stored as a string , allowing for a fixed decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter *The integer types have an extra option called UNSIGNED. Normally, the integer goes from an negative to positive value. Adding the UNSIGNED attribute will move that range up so it starts at zero instead of a negative number.
Use the PHP mysqli_connect() function to open a new connection to the MySQL server +
mysqli_connect(host,username,password,dbname); mysql_select_db($dbname); // Create connection $con=mysqli_connect("example.com","peter","abc123","my_db"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // Create database $sql="CREATE DATABASE my_db"; if (mysqli_query($con,$sql)) { echo "Database my_db created successfully"; } else { echo "Error creating database: " . mysqli_error($con); } // Create table $sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)"; // Execute query if (mysqli_query($con,$sql)) { echo "Table persons created successfully"; } else { echo "Error creating table: " . mysqli_error($con); } // close connection mysqli_close($con); -------------------------------------------------- $hostname='mizan.db.7901658.hostedresource.com'; $username='mizan'; $password='TillaBari3200'; $dbname='mizan'; mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.'); mysql_select_db($dbname); $result = mysql_query("select * from solicitor where areaoflaw like '%family%' "); // Display data in a table while ($row = mysql_fetch_assoc($result)) { echo"<tr><td>".$row['firmname']."</td><td>".$row['address']."</td><td>".$row['telephone']."</td><td>".$row['website']."</td></tr>"; //echo"<tr><td>".$row['firmname']."</td><td>".$row['areaoflaw']."</td></tr>"; }
Primary Keys and Auto Increment Fields +
Each table in a database should have a primary key field.
A primary key is used to uniquely identify the rows in a table. Each primary key value must be unique within the table. Furthermore, the primary key field cannot be null because the database engine requires a value to locate the record.
The following example sets the PID field as the primary key field. The primary key field is often an ID number, and is often used with the AUTO_INCREMENT setting. AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field:
$sql = "CREATE TABLE Persons ( PID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(PID), FirstName CHAR(15), LastName CHAR(15), Age INT )";
MySql query with php +
// Connects to your Database mysqli_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); mysqli_select_db("Database_Name") or die(mysql_error()); $data = mysqli_query("SELECT * FROM friends WHERE pet='Cat'") or die(mysql_error()); while($info = mysqli_fetch_array( $data )) // or while ($row = mysql_fetch_assoc($data)) // or while ($line = mysql_fetch_array($data, MYSQL_ASSOC)) { Print "<tr>"; Print "<th>Name:</th> <td>".$info['name'] . "</td> "; Print "<th>Color:</th> <td>".$info['fav_color'] . "</td> "; Print "<th>Food:</th> <td>".$info['fav_food'] . "</td> "; Print "<th>Pet:</th><td>".$info['pet'] . " </td></tr>"; }
What is collation in MySQL Database? +
Collation in MySQL Database is a set of rules used in comparisons. Because many people use MySQL with data to be stored in languages other than English, they need to select the rules of comparisons which in turn depends on the character set used for storing that data.
In MySQL, data is stored using a specific character set, which can be defind at different levels; i.e., the sever, the database, the table, and the column levels. Each character set has a default collation; for instance, the Latine1 character set uses the latin1_swedish_ci collation which is the Swedish case insensitive order. Usually, when someone develops an application that involves localization (using the local language; e.g., Arabic) or Internationalization (i.e., using multiple languages), they resort to Unicode (utf-8) which has several collations. In general, it is a good idea to set the character set to utf-8 and select the relevant collation or just accept the default utf8-general-ci.
Reference :
[1] MySQL Reference, Character Sets and Collations in General. Available at http://dev.mysql.com/doc/refman/5.0/en/charset-general.html