Sunday, October 28, 2018

Create a MySQL Database Using MySQLi and PDO

The following examples create a database named "myDB":


<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection$conn = new mysqli($servername, $username, $password);
// Check connectionif ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// Create database$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully";
else {
    echo "Error creating database: " . $conn->error;
}

$conn->close();
?>



Create a MySQL Table Using MySQLi and PDO:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection$conn = new mysqli($servername, $username, $password, $dbname);
// Check connectionif ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// sql to create table$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)"
;

if ($conn->query($sql) === TRUE) {
    echo "Table MyGuests created successfully";
else {
    echo "Error creating table: " . $conn->error;
}

$conn->close();
?>


Related Posts:

  • Simple Login with php & mysql Creating the Database Table:- Execute the following SQL query to create the users table inside your MySql database. CREATE TABLE users ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL UN… Read More
  • Create a MySQL Database Using MySQLi and PDO The following examples create a database named "myDB": <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection$conn = new mysqli($servername, $us… Read More
  • History Of PHP The History Of Php  PHP is an "HTML-embedded scripting language" primarily used for dynamic Web applications. The first part of this definition means that PHP code can be interspersed with HTML, making it sim… Read More
  • Php Mysql Login Video Tutorial Read More
  • Welcome To My Blog                                                               … Read More

0 comments:

Post a Comment