Creating MySQL database using PHP - LEARNING PHP AND MYSQL (2015)

LEARNING PHP AND MYSQL (2015)

Creating MySQL database using PHP

Admin should be privilege to create database. PHP uses mysql_query for creation of the SQL database. This has two parameter functioning which is true when creation is successful and false when a failure occurs.

Syntax

Bool mysql_query (sql, connection);

Example

<? php

$dbhost = ‘localhost :30306’ ;

$dbuser =’root’;

$dbpass = ‘password’

$conn = mysql_connection ($dbhost, $dbuser, $dbpass);

If (!conn)

{

Die (‘could not connect’. mysql_error ());

}

Echo ‘connect success’;

$sql = ‘create database test-tb’;

$retreval = mysql_query ($sql, $conn);

If ( ! $retval)

{

Die (‘could not create database‘. mysql_error () );

}

Echo “database test_db created database\n”;

Mysql_close ($conn);

?>

Insert of data in MySQL database using PHP

The insert statement carries out the function using PHP by using query statement mysql_query for the SQL INSERT statement.

Example

<? php

$dbhost = 'localhost: 3036’;

$dbuser = 'root';

$dbpass = 'password';

$conn = mysql_connect ($dbhost, $dbuser, $dbpass);

If (! $conn)

{

Die ('Could not connect’. mysql_error ());

}

$sql = 'INSERT INTO employee '.

'(emp_name, emp_address, emp_salary, join_date) '.

'VALUES (“guest", "XYZ", 2000, NOW ())';

mysql_select_db ('test_db');

$retval = mysql_query ($sql, $conn);

If (! $retval)

{

Die (‘could not enter data: ‘. mysql_error ());

}

Echo "Entered data successfully\n";

mysql_close ($conn);

?>