PHP and 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,&quot;root&quot;,&quot;&quot;); if (!$link) {die(&quot;Could not connect to MySQL: &quot; . mysql_error()); } echo &quot;Connection OK&quot;; 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,&quot;root&quot;,“root&quot;); $sql =  &quot;CREATE DATABASE PWWWSample&quot;; mysql_query ($sql) or die(mysql_error()); echo(&quot;Database Created&quot;); ?> Open a connection to MySQL Create the Data Definition Query Run Query or Capture any error and display reason. Give feedback to the user
Create database (phpMyAdmin) 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,&quot;root&quot;,“root&quot;); Sets up a connection object ($conn) that represents a connection to the database server mysql_select_db(&quot;pwwwsample&quot;,$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,&quot;root&quot;,“root&quot;); mysql_select_db(&quot;pwwwsample&quot;,$conn); $sql = &quot;CREATE TABLE student( studentId int not null primary key, surname varchar (25), firstname varchar (25) )&quot;; 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,&quot;root&quot;,&quot;&quot;); mysql_select_db(&quot;pwwwsample&quot;,$conn); $sql = &quot;INSERT INTO student VALUES ( ‘ 100 ’ , ‘ Blakeway ’ , ‘ Stewart ’ )&quot;; 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 = &quot;INSERT INTO student VALUES (‘100’,‘Blakeway’,‘Stewart’)&quot;; 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 ’ )&quot;;
User input <html> <body> <form action=&quot;form_insert.php&quot; method=&quot;post&quot;> Name: <input type=&quot;text&quot; name=&quot;username&quot; /> <input type=&quot;submit&quot; /> </form> </body> </html> Simple_form_insert.html
Processing data from forms <?php  $con = mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;root&quot;); if (!$con) { exit('Could not connect: ' . mysql_error()); }/mysql_select_db(&quot;test_database&quot;,$con); mysql_query(&quot;INSERT INTO users (userName) VALUES ('&quot; . $_POST[&quot;username&quot;] . &quot;')&quot;); mysql_close($con); ?> <html> <head></head> <body> <p>Thankyou <?php print $_POST[&quot;username&quot;] ?></p> </body> </html> Simple_form_insert.php

Database presentation

  • 1.
  • 2.
    What will wecover Basic SQL commands Create Database Create Table Insert data
  • 3.
    MySQL MySQL isan 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
  • 4.
    Overall approach Createa 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
  • 5.
    Creating a connectionto 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
  • 6.
    Testing the connectionTo test the database connectivity you can check the status. You do not need to specify a database here. <?php $link = Mysql_connect(localHost,&quot;root&quot;,&quot;&quot;); if (!$link) {die(&quot;Could not connect to MySQL: &quot; . mysql_error()); } echo &quot;Connection OK&quot;; mysql_close($link); ?> Should echo out: Connection OK or Could not connect to MySQL if there is a problem.
  • 7.
    Creating a databaseDirectly in SQL Using an administration client E.g. phpMyAdmin- built into Uniform Server Produces the SQL for you
  • 8.
    Create database (directlyin SQL) <?PHP mysql_connect (localhost,&quot;root&quot;,“root&quot;); $sql = &quot;CREATE DATABASE PWWWSample&quot;; mysql_query ($sql) or die(mysql_error()); echo(&quot;Database Created&quot;); ?> Open a connection to MySQL Create the Data Definition Query Run Query or Capture any error and display reason. Give feedback to the user
  • 9.
    Create database (phpMyAdmin)See separate guide in the resources section of the website for detailed instructions
  • 10.
    Creating a tableDirectly 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)
  • 11.
    Selecting the databaseThere could be many databases on the database server. You need to ensure you select the correct one: $conn=mysql_connect (localhost,&quot;root&quot;,“root&quot;); Sets up a connection object ($conn) that represents a connection to the database server mysql_select_db(&quot;pwwwsample&quot;,$conn); Selects the appropriate database using the connection object
  • 12.
    Constructing the SQLQuery 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 )
  • 13.
    Executing the SQLQuery $conn = mysql_connect(localHost,&quot;root&quot;,“root&quot;); mysql_select_db(&quot;pwwwsample&quot;,$conn); $sql = &quot;CREATE TABLE student( studentId int not null primary key, surname varchar (25), firstname varchar (25) )&quot;; mysql_query ($sql) or die(mysql_error());
  • 14.
    Retrieving Error MessagesTo ensure that the SQL works you can retrieve error messages and display them $result = mysql_query ($sql, $conn) or die(mysql_error() ); echo $result;
  • 15.
    So far: Wehave 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
  • 16.
    Inserting DATA Thenext step is to insert data into your table. $conn=mysql_connect (localhost,&quot;root&quot;,&quot;&quot;); mysql_select_db(&quot;pwwwsample&quot;,$conn); $sql = &quot;INSERT INTO student VALUES ( ‘ 100 ’ , ‘ Blakeway ’ , ‘ Stewart ’ )&quot;; mysql_query ($sql) or die(mysql_error());
  • 17.
    Columns Notice withthe SQL below we have not specified which columns we wish to enter data into. $sql = &quot;INSERT INTO student VALUES (‘100’,‘Blakeway’,‘Stewart’)&quot;; Because we wish to enter data into the columns in the same order as they appear on the database this is acceptable.
  • 18.
    Columns If wedo 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 ’ )&quot;;
  • 19.
    User input <html><body> <form action=&quot;form_insert.php&quot; method=&quot;post&quot;> Name: <input type=&quot;text&quot; name=&quot;username&quot; /> <input type=&quot;submit&quot; /> </form> </body> </html> Simple_form_insert.html
  • 20.
    Processing data fromforms <?php $con = mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;root&quot;); if (!$con) { exit('Could not connect: ' . mysql_error()); }/mysql_select_db(&quot;test_database&quot;,$con); mysql_query(&quot;INSERT INTO users (userName) VALUES ('&quot; . $_POST[&quot;username&quot;] . &quot;')&quot;); mysql_close($con); ?> <html> <head></head> <body> <p>Thankyou <?php print $_POST[&quot;username&quot;] ?></p> </body> </html> Simple_form_insert.php