Database presentation


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Database presentation

  1. 1. PHP and databases
  2. 2. What will we cover <ul><li>Basic SQL commands </li></ul><ul><ul><li>Create Database </li></ul></ul><ul><ul><li>Create Table </li></ul></ul><ul><ul><li>Insert data </li></ul></ul>
  3. 3. MySQL <ul><li>MySQL is an example of a database server </li></ul><ul><ul><li>Designed for remote connections </li></ul></ul><ul><ul><li>No user interface unless you install a local client or connect from a web form (e.g. phpMyAdmin) </li></ul></ul><ul><ul><li>Others include MS SQL Server, PostgreSQL </li></ul></ul><ul><li>Runs alongside the Apache web server when Uniform Server is started </li></ul>
  4. 4. Overall approach <ul><li>Create a connection with the database Server </li></ul><ul><li>Create database/select database/create table(s) </li></ul><ul><li>Construct SQL query as a string in PHP </li></ul><ul><li>Execute the SQL query in PHP </li></ul>
  5. 5. Creating a connection to the database server <ul><li>mysql_connect($host,$user,$password); </li></ul><ul><li>host is the SQL server address </li></ul><ul><li>user is the username to login to the database </li></ul><ul><ul><li>Default is root in Uniform Server. This is the user with most privileges. </li></ul></ul><ul><li>password is the password associated with the username </li></ul><ul><ul><li>Default is root in Uniform Server </li></ul></ul>
  6. 6. Testing the connection <ul><li>To test the database connectivity you can check the status. You do not need to specify a database here. </li></ul><ul><ul><li><?php $link = Mysql_connect(localHost,&quot;root&quot;,&quot;&quot;); </li></ul></ul><ul><ul><li>if (!$link) {die(&quot;Could not connect to MySQL: &quot; . mysql_error()); } echo &quot;Connection OK&quot;; mysql_close($link); ?> </li></ul></ul><ul><li>Should echo out: Connection OK or Could not connect to MySQL if there is a problem. </li></ul>
  7. 7. Creating a database <ul><li>Directly in SQL </li></ul><ul><li>Using an administration client </li></ul><ul><ul><li>E.g. phpMyAdmin- built into Uniform Server </li></ul></ul><ul><ul><li>Produces the SQL for you </li></ul></ul>
  8. 8. Create database (directly in SQL) <ul><li><?PHP </li></ul><ul><li>mysql_connect (localhost,&quot;root&quot;,“root&quot;); </li></ul><ul><li>$sql = &quot;CREATE DATABASE PWWWSample&quot;; </li></ul><ul><li>mysql_query ($sql) or die(mysql_error()); </li></ul><ul><li>echo(&quot;Database Created&quot;); </li></ul><ul><li>?> </li></ul>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. 9. Create database (phpMyAdmin) See separate guide in the resources section of the website for detailed instructions
  10. 10. Creating a table <ul><li>Directly in SQL </li></ul><ul><ul><li>Create a connection with the database server </li></ul></ul><ul><ul><li>Select the appropriate database </li></ul></ul><ul><ul><li>Construct the SQL query </li></ul></ul><ul><ul><li>Execute the SQL query </li></ul></ul><ul><li>phpMyAdmin (see separate guide) </li></ul>
  11. 11. Selecting the database <ul><li>There could be many databases on the database server. You need to ensure you select the correct one: </li></ul><ul><li>$conn=mysql_connect (localhost,&quot;root&quot;,“root&quot;); </li></ul><ul><ul><li>Sets up a connection object ($conn) that represents a connection to the database server </li></ul></ul><ul><li>mysql_select_db(&quot;pwwwsample&quot;,$conn); </li></ul><ul><ul><li>Selects the appropriate database using the connection object </li></ul></ul>
  12. 12. Constructing the SQL Query <ul><li>You are expected to design the relational database with tables and fields </li></ul><ul><li>Once the design is complete you can implement the physical design using PHP and MySQL </li></ul><ul><li>$sql = “CREATE TABLE tablename ( </li></ul><ul><li>fieldname1 datatype, </li></ul><ul><li>fieldname2 datatype </li></ul><ul><li>) </li></ul>
  13. 13. Executing the SQL Query <ul><li>$conn = mysql_connect(localHost,&quot;root&quot;,“root&quot;); </li></ul><ul><li>mysql_select_db(&quot;pwwwsample&quot;,$conn); </li></ul><ul><li>$sql = &quot;CREATE TABLE student( </li></ul><ul><li>studentId int not null primary key, </li></ul><ul><li>surname varchar (25), </li></ul><ul><li>firstname varchar (25) </li></ul><ul><li>)&quot;; </li></ul><ul><li>mysql_query ($sql) or die(mysql_error()); </li></ul>
  14. 14. Retrieving Error Messages <ul><li>To ensure that the SQL works you can retrieve error messages and display them </li></ul><ul><li>$result = mysql_query ($sql, $conn) or die(mysql_error() ); </li></ul><ul><li>echo $result; </li></ul>
  15. 15. So far: <ul><li>We have connected to the SQL Server </li></ul><ul><li>We have created a new database </li></ul><ul><li>We have selected the correct database </li></ul><ul><li>We have created a table called student with 3 fields. </li></ul><ul><ul><ul><li>studentId </li></ul></ul></ul><ul><ul><ul><li>surname </li></ul></ul></ul><ul><ul><ul><li>firstname </li></ul></ul></ul>
  16. 16. Inserting DATA <ul><li>The next step is to insert data into your table. </li></ul><ul><li>$conn=mysql_connect (localhost,&quot;root&quot;,&quot;&quot;); </li></ul><ul><li>mysql_select_db(&quot;pwwwsample&quot;,$conn); </li></ul><ul><li>$sql = &quot;INSERT INTO student VALUES ( ‘ 100 ’ , ‘ Blakeway ’ , ‘ Stewart ’ )&quot;; </li></ul><ul><li>mysql_query ($sql) or die(mysql_error()); </li></ul>
  17. 17. Columns <ul><li>Notice with the SQL below we have not specified which columns we wish to enter data into. </li></ul><ul><ul><li>$sql = &quot;INSERT INTO student VALUES (‘100’,‘Blakeway’,‘Stewart’)&quot;; </li></ul></ul><ul><li>Because we wish to enter data into the columns in the same order as they appear on the database this is acceptable. </li></ul>
  18. 18. Columns <ul><li>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. </li></ul><ul><li>$sql = INSERT INTO student (studentId, surname, firstname) </li></ul><ul><li>VALUES ( ‘ 100 ’ , ‘ Blakeway ’ , ‘ Stewart ’ )&quot;; </li></ul>
  19. 19. User input <ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><form action=&quot;form_insert.php&quot; method=&quot;post&quot;> </li></ul><ul><li>Name: <input type=&quot;text&quot; name=&quot;username&quot; /> </li></ul><ul><li><input type=&quot;submit&quot; /> </li></ul><ul><li></form> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>Simple_form_insert.html
  20. 20. Processing data from forms <ul><li><?php </li></ul><ul><li>$con = mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;root&quot;); </li></ul><ul><li>if (!$con) </li></ul><ul><li>{ </li></ul><ul><li>exit('Could not connect: ' . mysql_error()); </li></ul><ul><li>}/mysql_select_db(&quot;test_database&quot;,$con); </li></ul><ul><li>mysql_query(&quot;INSERT INTO users (userName) VALUES ('&quot; . $_POST[&quot;username&quot;] . &quot;')&quot;); </li></ul><ul><li>mysql_close($con); </li></ul><ul><li>?> </li></ul><ul><li><html> </li></ul><ul><li><head></head> </li></ul><ul><li><body> </li></ul><ul><li><p>Thankyou <?php print $_POST[&quot;username&quot;] ?></p> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>Simple_form_insert.php