PHP has three different ways to connect to and interact with a MySQL database: the original MySQL extension (with functions), MySQL Improved (MySQLi, object-oriented), or PHP Data Objects (PDO, object-oriented).
They can't be mixed in the same script. The original MySQL extension is no longer actively developed and is not recommended for new PHP-MySQL projects.
The PHP documentation describes MySQLi as the preferred option recommended by MySQL for new projects.
$conn = new mysqli($servername, $username, $password, $database, $port);- $servername - Specifies the server to connect to. If you pass the NULL value or an empty string "", the server will use the default value: "localhost"
<?php // connect to the server $conn = new mysqli('localhost', 'root', 'pass', 'dbname'); // check connection if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); } ?>- This connects to a database called "dbname", and stores the connection object as $conn.
<?php // connect to the server $conn = new mysqli('localhost', 'root', 'pass', 'dbname'); // check connection if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); } // some code $conn->close(); // close the connection // other PHP code ?>
CREATE DATABASE database_nameTo get PHP to execute the SQL instructions, first you must create a mysqli object with the conection to the server, then use the query() method of the MySQLi class.
mysqliObj->query($sql_query)- mysqliObj - is the mysqli object created with new mysqli()
<?php // connect to the MySQL server $conn = new mysqli('localhost', 'root', 'pass'); // check connection if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); } // sql query with CREATE DATABASE $sql = "CREATE DATABASE `tests` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci"; // Performs the $sql query on the server to create the database if ($conn->query($sql) === TRUE) { echo 'Database "tests" successfully created'; } else { echo 'Error: '. $conn->error; } $conn->close(); ?>- When you want to create a new database, you must add only the first three arguments to the mysqli object (servername, username and password), but, if you have to use a specific port, add an empty string "" for the database-name argument:
SQL commands are case-insensitive, so, you can use "CREATE DATABASE" or "create database". But the name of tables and columns are case-sensitive.
CREATE TABLE `table_name` ( `column_name1` data_type, `column_name2` data_type, `column_name3` data_type, .... )The data type specifies what type of data the column can hold. For a list of the MySQL data types, see the previous lesson: PHP MySQL Introduction, Data Types.
<?php // connect to the MySQL server $conn = new mysqli('localhost', 'root', 'pass', 'tests'); // check connection if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); } // sql query for CREATE TABLE $sql = "CREATE TABLE `users` ( `id` INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(25) NOT NULL, `pass` VARCHAR(18) NOT NULL, `email` VARCHAR(45), `reg_date` TIMESTAMP ) CHARACTER SET utf8 COLLATE utf8_general_ci"; // Performs the $sql query on the server to create the table if ($conn->query($sql) === TRUE) { echo 'Table "users" successfully created'; } else { echo 'Error: '. $conn->error; } $conn->close(); ?>- This code will create a table called "users" in the "tests" database, with five columns (id , name , pass , email and reg_date), sets the 'id' column as the primary key field.
In SQL statements is recommended that the names of the tables and columns to be added within apostrophes ` ` (carefully, not single quotes, but the character near the key with the number 1 on the keyboard). This is the correct syntax, but is not strictly required.
<input type="checkbox" name="a_name" value="value" checked="checked" />
#id { background:url("path_to_image.png"); background-size:contain; background-repeat:no-repeat; }
var rest8_7 = 8 % 7; alert(rest8_7);
$nr = ceil(3.5); echo $nr; // 4