database presentation

20
PHP and databases

Upload: webhostingguy

Post on 17-May-2015

11.957 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Database presentation

PHPand databases

Page 2: Database presentation

What will we cover

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

Page 3: Database presentation

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

Page 4: Database presentation

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

Page 5: Database presentation

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

Page 6: Database presentation

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.

Page 7: Database presentation

Creating a database

• Directly in SQL

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

Page 8: Database presentation

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

Page 9: Database presentation

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

Page 10: Database presentation

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)

Page 11: Database presentation

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

Page 12: Database presentation

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

)

Page 13: Database presentation

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());

Page 14: Database presentation

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;

Page 15: Database presentation

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

Page 16: Database presentation

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());

Page 17: Database presentation

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.

Page 18: Database presentation

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’)";

Page 19: Database presentation

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

Page 20: Database presentation

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