Q7. Write PHP Script for the following Marks [1] Creating connection with MySQL
ID: 3571176 • Letter: Q
Question
Q7. Write PHP Script for the following Marks [1]
Creating connection with MySQL
Creating database tables Employee(emp_id,emp_name,emp_address,emp_salary,join_date,salary)
Select database table
7. Write PHP Script for the following Marks [1] Creating connection with MySQL Creating database tables Employee emp id,emp name,emp address,emp salaryjoin date,salary) ii) Select database table Employee emp id,emp name emp address,emp salaryjoin date,salary)Explanation / Answer
1-Following is the program of creating database in php-
<?php
$dbhostname = 'localhost';
$dbusername = 'root';
$dbpassword = 'root';
$conn = mysql_connect($dbhostname, $dbusername, $dbpassword);
if(! $conn ) {
die('No connection: ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE Database databasenmae';
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not create database: ' . mysql_error());
}
echo "Database databasename is created successfully ";
mysql_close($conn);
?>
2-Following is the program of connecting to the created database-
<?php
$dbhostname = 'localhost';
$dbusername = 'yourname';
$dbpassword = 'yourname789';
$conn = mysql_connect($dbhostname, $dbusername, $dbpassword);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db( 'databasename' );
mysql_close($conn);
?>
3-Following is the program of creating table in created database-
<?php
$dbhostname = 'localhost';
$dbusername = 'root';
$dbpassword = 'root';
$conn = mysql_connect($dbhostname, $dbusername, $dbpassword);
if(! $conn ) {
die('No connection: ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE TABLE employee( '.
'emp_id INT NOT NULL AUTO_INCREMENT, '.
'emp_name VARCHAR(20) NOT NULL, '.
'emp_address VARCHAR(20) NOT NULL, '.
'emp_salary INT NOT NULL, '.
'join_date timestamp(14) NOT NULL, '.
'primary key ( emp_id ))';
mysql_select_db('databasename');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not create table: ' . mysql_error());
}
echo "Table employee created successfully ";
mysql_close($conn);
?>
4-Following is the program of selecting table from created database-
<?php
$dbhostname = 'localhost';
$dbusername = 'root';
$dbpassword = 'rootpassword';
$conn = mysql_connect($dbhostname, $dbusername, $dbpassword);
if(! $conn )
{
die('No connection: ' . mysql_error());
}
$sql = 'SELECT emp_id,emp_name,emp_address,emp_salary,join_date
FROM employee ';
mysql_select_db('databasename');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
echo "Employee ID :{$row['emp_id']} <br> ".
"Employee name: {$row['emp_name']} <br> ".
"Address: {$row['emp_address']} <br> ".
"Salary: {$row['emp_salary']} <br> ".
"Join Date : {$row['join_date']} <br> ".
"---------------------------------------------<br>";
}
echo "Fetched data successfully ";
mysql_close($conn);
?>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.