When we see PHP data types, we have listed a set of possible types of data with which the PHP variable may be initialized. Similarly, MySQL supports a huge list of data types, and it is mandatory to specify the data type of each attribute of a table, unlike PHP, a loosely typed language that we have seen with PHP Data Type Conversion.
MySQL-supported data types that are huge in number, fall into the three main categories. These are,
On creating a MySQL table with attributes with a numeric data type, then this attribute will accept all data containing only signed and unsigned numbers.
These number values can be any one of the types, like, integers, floating-point numbers, decimals, booleans and etc. Again the list of MySQL-supported numeric data types falls into the following categories.
MySQL provides more string data-type, like CHAR, VARCHAR, TEXT and etc., as shown below.
This kind of MySQL data type is provided for representing temporal data that are used for MySQL date/time manipulation. Common Date and Time-related types supported by MySQL are as follows.
Now, we are going to write a MySQL query for creating a table by specifying table attributes and their corresponding data types.
CREATE TABLE event(name VARCHAR(255), date DATETIME, status ENUM(ACTIVE’,’INACTIVE’));
The above query will create a new table named an event containing the event name, event date and its status with the data types as specified. And, the table structure, after executing this query is shown below.
Now, let us insert a row of data into the event table by executing the following query.
INSERT INTO event (name, date, status) VALUES ('Birthday Party', '2013-07-06 18:30:00', 'ACTIVE');
Since the values set for the INSERT query are with the right data type as specified in creating the event table, the above query will successfully be executed and a new row will be added as a result.
If we set query data with improper data type not matched with an actual attribute data type, for example, if we set the value event status which neither ACTIVE nor INACTIVE, let us examine what would happen. Now the query is,
INSERT INTO event (name, date, status) VALUES ('Birthday Party', '2013-07-06 18:30:00', 'TRUE');
While executing the above query for trying to store value TRUE for event status, it will fail, rather store NULL value instead.
Important: If you are going to load the user-entered data into the query, then using a prepared statement is mandatory.
This example handles the user-submitted data with a prepared statement. It will show the behavior of MySQL insert with respect to the data type configured.
The select option of the form has the valid ENUM options and also an invalid option. If the user submits invalid data, then the INSERT operations will not be performed.
<?php
if (isset($_POST['insert-event'])) {
$conn = mysqli_connect("localhost", "root", "", "sql_data_type");
$sql = $conn->prepare("INSERT INTO event (name, date, status) VALUES (?, ?, ?)");
$name = $_POST["event_name"];
$date = $_POST["event_date"];
$status = $_POST["event_status"];
$sql->bind_param("sss", $name, $date, $status);
$result = $sql->execute();
}
?>
<form method="post">
<input type="text" name="event_name" placeholder="Name" value="Vincy" />
<input type="date" name="event_date"
value="<?php echo date('Y-m-d'); ?>" /> <select name="event_status">
<option value="ACTIVE">ACTIVE</option>
<option value="INACTIVE">INACTIVE</option>
<option value="Invalid">Invalid</option>
</select>
<input type="submit" name="insert-event" value="Insert Event" />
</form>