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

Php and Mysql

plearn

Uploaded by

bineb39025
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Php and Mysql

plearn

Uploaded by

bineb39025
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

<?

php

// Server connection parameters


$servername = "127.0.0.1";
$username = "root";
$password = '';
$dbname = "testdb";

// Create connection with the database


$conn = new mysqli($servername, $username, $password, $dbname);

// Check if connection was successful


if ($conn->connect_error) {
// Connection failed, display error message
die("Connection Failed: " . $conn->connect_error);
} else {
// Connection successful, notify user
echo "Connection successful!";
}

// SQL to create a new table named 'students'


$sql = "CREATE TABLE students (
ID INT NOT NULL,
LastName VARCHAR(255) NOT NULL,
FirstName VARCHAR(255),
Age INT CHECK (Age>=18),
PRIMARY KEY (ID)
)";

// Execute the query to create the table


if ($conn->query($sql) === TRUE) {
// Table creation successful
echo "Table created successfully!";
} else {
// Table creation failed, display error message
echo "Error in creating table " . $conn->error;
}

// SQL to insert a new record into the 'students' table


$sql = "INSERT INTO students (ID, LastName, FirstName, Age) VALUES
('1', 'Doe', 'John', 18),
('2', 'Smith', 'Jane', 22),
('3', 'Brown', 'Robert', 19),
('4', 'Davis', 'Emily', 20)";
// Execute the query to insert the data
if ($conn->query($sql) === TRUE) {
// Record insertion successful
echo "New Record created successfully!";
} else {
// Record insertion failed, display error message
echo "Error:" . $sql . "<br>" . $conn->error;
}

// SQL to select records from the 'students' table


$sql = "SELECT * FROM students";
$result = $conn->query($sql);

// Check if the selection returned any rows


if ($result->num_rows > 0) {
// Iterate over each row and display the data
while ($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"] . " - Name: " . $row["FirstName"] . " " .
$row["LastName"] . " " . $row["Age"] . "<br>";
}
} else {
// No records found, display a message
echo "0 results";
}

?>

The CHECK constraint in SQL is used to limit the value range that can be placed in a column. If you
define a CHECK constraint on a column (for the Age column in this case) with the condition CHECK
(Age>=18), it enforces that only values of 18 or higher can be inserted into this column.
If you attempt to insert a record with an Age value less than 18, the database will reject the insert
operation, and you will receive an error from the DBMS (Database Management System). This error will
prevent the entire SQL statement from executing, so even if there are multiple rows being inserted with
one INSERT command, none of the rows will be inserted if at least one row violates the CHECK
constraint.
For instance, if you have the following SQL statement:

$sql = "INSERT INTO students (ID, LastName, FirstName, Age) VALUES


('1', 'Doe', 'John', 17)"; // 17 is less than the required 18

The database will return an error similar to this:


Error: Check constraint violation: 'students_chk_1' for row 1

You might also like