PHP UNIT 5
PHP UNIT 5
Unit 5: MYSQL
Example
Try out following example to create a table −
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
if(! $retval ) {
die('Could not create table: ' . mysql_error());
}
mysql_close($conn);
?>
In case you need to create many tables then its better to create a text file
first and put all the SQL commands in that text file and then load that file into
$sql variable and excute those commands.
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$query_file = 'sql_query.txt';
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not create table: ' . mysql_error());
}
"localhost/phpmyadmin"
Steps in Detail:
Now open your PHP file and write your PHP code to create database
and a table in your database.
<?php
// Password is empty
$password = "";
// Check connection
if ($conn->connect_error) {
die("Connection failure: "
. $conn->connect_error);
}
// Closing connection
$conn->close();
?>
2. INT :he INTEGER data type accepts numeric values with an implied
scale of zero. It stores any integer value between -2147483648 to
2147483647.
The attributes that are used along with data types in this article are:
1. NOT NULL: Each row must contain a value for that column, null values
are not allowed.
2. PRIMARY KEY: Used to uniquely identify the rows in a table. The column
with PRIMARY KEY setting is often an ID number.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "newDB";
// checking connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>