How to create a MySQL Database and Table in PHP

MySQL Database and Table in PHP

Article on how to create a MySQL Database and Table in PHP I'd give how to create tables, create the database, and choose MySQL database in PHP.

How to Create a MySQL Database in PHP

PHP using the mysql_query function to create a MySQL database, this function takes two parameters and returns TRUE if successful or FALSE if it fail.

PHP Script :
bool mysql_query( sql, connection );

The description of the parameter in the script above is:

  • sql - This parameter is required to put the SQL query to create the database.
  • connection- This parameter is optional, if not specified, then it will use mysql_connect
function connection with the most recent. Note the example scripts create MySQL databases in PHP:

PHP Script :
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$koneksi = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $koneksi )
{
  die('connection fail: ' . mysql_error());
}
echo 'Connection Success';
$sql = 'CREATE Database test_db';
$buatdb = mysql_query( $sql, $conndb );
if(! $buatdb )
{
  die('Creating a database, fail: ' . mysql_error());
}
echo "Database test_db created\n";
mysql_close($conndb);
?>

How to choose MySQL Database in PHP

After you make a connection with a server database the next step i.e. choosing a database, it is necessary because there may be multiple databases that reside on the server of the parent and we can do the work with a database that has been selected at a time.

PHP provides functions to select mysql_select_db database. This function returns TRUE if successful or FALSE if it fails.

PHP Script :
bool mysql_select_db( db_name, connection )

The description of the parameter in the script above is:

  • db_name – this parameter is required for the name of the database that will be selected.
  • connection – this parameter is optional, if not specified, then it will use mysql_connect function connection with the most recent.

Note the example scripts select MySQL database in PHP:

PHP Script :
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$koneksi = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $koneksi )
{
  die('Connection Fail: ' . mysql_error());
}
echo 'Connection Succes';
mysql_select_db( 'test_db' );
mysql_close($conndb);
?>

How to create a table in a MySQL Database with PHP

To create tables in a database, we need to do as we create the database. First, we create a SQL query to create the table and then run the query using the function mysql_query ().

Note the example scripts create tables in a MySQL database with PHP:

PHP Script :
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conndb )
{
  die('Connection Failed: ' . mysql_error());
}
echo 'Connection Succes';
$sql = 'CREATE TABLE worker( '.
       'id_worker INT NOT NULL AUTO_INCREMENT, '.
       'name_worker VARCHAR(20) NOT NULL, '.
       'address_worker  VARCHAR(20) NOT NULL, '.
       'salery_worker   INT NOT NULL, '.
       'date_joined    timestamp(14) NOT NULL, '.
       'primary key ( id_worker ))';

mysql_select_db('test_db');
$maketable = mysql_query( $sql, $conndb);
if(! $maketable )
{
  die('Failed to create a table: ' . mysql_error());
}
echo "The worker table has been created\n";
mysql_close($conndb);
?>

Note the contents of the $sql, on the variable contains the SQL query to create the table. Thus the article How to Create a MySQL Database and Table in PHP. May be useful for you.



Latest
Previous
Next Post »