AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP MySQLi create DataBase


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

PHP Tutorial » PHP MySQLi create DataBase

To create a database you must have admin privilege. It's easy to create a new MySQL database. PHP uses query function to create a MySQL database. This function takes two parameters and returns TRUE on success or FALSE on failure.

In this chapter as well as in the following chapters we will show you three methods of working with PHP MySQLi create DataBase:

 

Create DataBase MySQLi (object oriented)

The following examples create a database named "Users":

Example (this is HTML - PHP editor, change text on this window)

<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
// Create connection
$link = new mysqli($server_name, $user_name,
$password);
// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}

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

$link->close();
?>
Note: When you create a new database, you must specify only: server_name, user_name and password).

 

Related subjects:
Insert multiple rows PHP select / view data Conect to MySQLi

Tags: how to create database in mysql using php code, mysqli create database if not exists, php create table, mysql create database php mysql tutorial create database end table in phpmyadmin

 

Create DataBase MySQLi (Procedural)

Attempt MySQL server connection. Assuming you are running MySQL server with default setting ("localhost", "root", "your_password")

The following examples create a database named "Members" using MySQLi (Procedural)

Example (this is HTML - PHP editor, change text on this window)

<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
// Create connection
$link = mysqli_connect($server_name, $user_name, $password);
// Check connection
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}

// Create database
$sql = "CREATE DATABASE Members";
if (mysqli_query($link, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($link);
}

mysqli_close($link);
?>

 

Create DataBase using (PDO) extension.

PDO extension. Attempt MySQL server connection. Assuming you are running MySQL server with default setting ("localhost", "root", "your_password")

The next PDO example create a DBase "my_DB_PDO":

Example (this is HTML - PHP editor, change text on this window)

<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
try {
$link = new PDO("mysql:host=$server_name", $user_name, $password);
// set the PDO error mode to exception
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE my_DB_PDO";
// use exec() because no results are returned
$link->exec($sql);
echo "Database created successfully<br>";
} catch(PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}

$link = null;
?>

 



how to create database in mysql using php code, mysqli create database if not exists, php create table, mysql create database php mysql tutorial create database end table in phpmyadmin
PHP MySQLi create DataBase - php tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...