Database presentation

Preview:

Citation preview

PHPand databases

What will we cover

• Basic SQL commands– Create Database– Create Table– Insert data

MySQL

• MySQL is an example of a database server – Designed for remote connections– No user interface unless you install a local

client or connect from a web form (e.g. phpMyAdmin)

– Others include MS SQL Server, PostgreSQL

• Runs alongside the Apache web server when Uniform Server is started

Overall approach

• Create a connection with the database Server• Create database/select database/create table(s)• Construct SQL query as a string in PHP• Execute the SQL query in PHP

Creating a connection to the database server

mysql_connect($host,$user,$password);• host is the SQL server address• user is the username to login to the

database– Default is root in Uniform Server. This is the

user with most privileges.

• password is the password associated with the username– Default is root in Uniform Server

Testing the connection

• To test the database connectivity you can check the status. You do not need to specify a database here.

<?php $link = Mysql_connect(localHost,"root","");

if (!$link) {die("Could not connect to MySQL: " . mysql_error()); } echo "Connection OK"; mysql_close($link); ?>

• Should echo out: Connection OK or Could not connect to MySQL if there is a problem.

Creating a database

• Directly in SQL

• Using an administration client– E.g. phpMyAdmin- built into Uniform Server– Produces the SQL for you

Create database (directly in SQL)<?PHP mysql_connect (localhost,"root",“root");

$sql = "CREATE DATABASE PWWWSample";

mysql_query ($sql) or die(mysql_error());

echo("Database Created");?>

Open a connection to MySQL

Open a connection to MySQL

Create the Data Definition Query

Create the Data Definition Query

Run Query or Capture any error and display reason.

Run Query or Capture any error and display reason.Give feedback to the userGive feedback to the user

Create database (phpMyAdmin)

See separate guide in the resources section of the website for detailed instructions

See separate guide in the resources section of the website for detailed instructions

Creating a table

• Directly in SQL– Create a connection with the database

server– Select the appropriate database– Construct the SQL query– Execute the SQL query

• phpMyAdmin (see separate guide)

Selecting the database

• There could be many databases on the database server. You need to ensure you select the correct one:

• $conn=mysql_connect (localhost,"root",“root");– Sets up a connection object ($conn) that represents a

connection to the database server

• mysql_select_db("pwwwsample",$conn);– Selects the appropriate database using the connection

object

Constructing the SQL Query

• You are expected to design the relational database with tables and fields

• Once the design is complete you can implement the physical design using PHP and MySQL

$sql = “CREATE TABLE tablename (

fieldname1 datatype,

fieldname2 datatype

)

Executing the SQL Query

$conn = mysql_connect(localHost,"root",“root");

mysql_select_db("pwwwsample",$conn);

$sql = "CREATE TABLE student(

studentId int not null primary key,

surname varchar (25),

firstname varchar (25)

)";

mysql_query ($sql) or die(mysql_error());

Retrieving Error Messages

• To ensure that the SQL works you can retrieve error messages and display them

$result = mysql_query ($sql, $conn) or die(mysql_error());

echo $result;

So far:

• We have connected to the SQL Server

• We have created a new database

• We have selected the correct database

• We have created a table called student with 3 fields.

• studentId• surname• firstname

Inserting DATA

The next step is to insert data into your table.

$conn=mysql_connect (localhost,"root","");mysql_select_db("pwwwsample",$conn);

$sql = "INSERT INTO student VALUES (‘100’,‘Blakeway’,‘Stewart’)";

mysql_query ($sql) or die(mysql_error());

Columns• Notice with the SQL below we have not

specified which columns we wish to enter data into.

$sql = "INSERT INTO student VALUES (‘100’,‘Blakeway’,‘Stewart’)";

• Because we wish to enter data into the columns in the same order as they appear on the database this is acceptable.

Columns

• If we do not wish to enter data into all columns or some columns are left null we must specify which columns are to have data inserted.

$sql = INSERT INTO student (studentId, surname, firstname)

VALUES (‘100’,‘Blakeway’,‘Stewart’)";

User input<html><body><form action="form_insert.php"

method="post">Name: <input type="text"

name="username" /><input type="submit" /></form></body></html>

Simple_form_insert.htmlSimple_form_insert.html

Processing data from forms<?php

$con = mysql_connect("localhost","root","root");

if (!$con)

{

exit('Could not connect: ' . mysql_error());

}/mysql_select_db("test_database",$con);

mysql_query("INSERT INTO users (userName) VALUES ('" . $_POST["username"] . "')");

mysql_close($con);

?>

<html>

<head></head>

<body>

<p>Thankyou <?php print $_POST["username"] ?></p>

</body>

</html>

Simple_form_insert.phpSimple_form_insert.php