Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP and MySQL.

Similar presentations


Presentation on theme: "PHP and MySQL."— Presentation transcript:

1 PHP and MySQL

2 MySQL Open Source database server
Runs on many platforms (Unix & Windows) Networked server – no fancy GUI like MS Access. You can find clients that provide a GUI. Great for small to medium-sized applications

3 MySQL Installation/Configuration
You install the server, and provide a root password. Now you need a client to do anything! Create databases, view databases, etc. Windows MySQL server comes with a command-line client You need to learn all the commands, and type them in manually…

4 phpMyAdmin A MySQL client written in PHP Via the web you can manage:
Manage Databases Manage MySQL users Submit queries (SQL) A great way to learn SQL!

5 Php – MySQL Support You need a version of PHP that includes the MySQL module Included by default on most php distributions. Documentation of all mysql functions/objects is available via php.net.

6 Opening a MySQL database
$username=“fred”; $password=“fred”; $database=“eiw”; mysql_connect("localhost",$username,$password); @mysql_select_db($database) or die( "Unable to select database"); Assumes that the machine running the server is “localhost” You can easily use a MySQL server that is running on a remote machine.

7 Submitting a query to the server
$query = "SELECT uid from users WHERE username = ‘fred’"; $res = mysql_query($query); // no result - no user so return false if (! $res) { … no result (error!) }

8 Accessing mysql_numrows($res) : number of rows in the result.
mysql_result($res,$index,$field) Returns a single column value $index is the row $field is the field name (column name)

9 Example $res = mysql_query(“SELECT * FROM users”);
if (! $res) { …handle error…} $numrows = mysql_numrows($res) for ($i=0;$i<$numrows;$i++) { $name = mysql_result($res,$i,”username”); $pass = mysql_result($res,$i,”password”); … Do something with $name and $password… }

10 Example – show data in the tables
Function: list all tables in your database. Users can select one of tables, and show all contents in this table. second.php showtable.php

11 second.php <html><head><title>MySQL Table Viewer</title></head><body> <?php // change the value of $dbuser and $dbpass to your username and password $dbhost = 'hercules.cs.kent.edu:3306'; $dbuser = 'nruan'; $dbpass = ‘*****************’; $dbname = $dbuser; $table = 'account'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if (!$conn) { die('Could not connect: ' . mysql_error()); } if (!mysql_select_db($dbname)) die("Can't select database");

12 second.php (cont.) $result = mysql_query("SHOW TABLES");
if (!$result) { die("Query to show fields from table failed"); } $num_row = mysql_num_rows($result); echo "<h1>Choose one table:<h1>"; echo "<form action=\"showtable.php\" method=\"POST\">"; echo "<select name=\"table\" size=\"1\" Font size=\"+2\">"; for($i=0; $i<$num_row; $i++) { $tablename=mysql_fetch_row($result); echo "<option value=\"{$tablename[0]}\" >{$tablename[0]}</option>"; echo "</select>"; echo "<div><input type=\"submit\" value=\"submit\"></div>"; echo "</form>"; mysql_free_result($result); mysql_close($conn); ?> </body></html>

13 showtable.php <html><head>
<title>MySQL Table Viewer</title> </head> <body> <?php $dbhost = 'hercules.cs.kent.edu:3306'; $dbuser = 'nruan'; $dbpass = ‘**********’; $dbname = 'nruan'; $table = $_POST[“table”]; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if (!$conn) die('Could not connect: ' . mysql_error()); if (!mysql_select_db($dbname)) die("Can't select database"); $result = mysql_query("SELECT * FROM {$table}"); if (!$result) die("Query to show fields from table failed!" . mysql_error());

14 showtable.php (cont.) $fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>"; echo "<table border='1'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td><b>{$field->name}</b></td>"; } echo "</tr>\n"; while($row = mysql_fetch_row($result)) { echo "<tr>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td>$cell</td>"; mysql_free_result($result); mysql_close($conn); ?> </body></html>

15 Functions Covered mysql_connect() mysql_select_db() include()
mysql_query() mysql_num_rows() mysql_fetch_array() mysql_close()

16 Project Simple e-commerce site Users login in (with just a user name)
View products (including search) Add products to shopping cart Remove products from shopping cart

17 Database Tables users – information about all the customers that use the system. products – information about all the products we sell. cartentries – shopping cart items (relates a user to a product)

18 Table: users uid: integer id number (autoincrement)
firstname, lastname: strings. varchar(20) username: string – login name. varchar(20) string. varchar(30)

19 Table: products pid: integer id number (autoincrement)
name: string – product name. varchar(30) price: floating point number.

20 Table: cartentries uid: integer user id number
pid: integer product id number quantity: integer (# products).

21 Some Queries Get list of all products:
“SELECT * FROM products” Get list of all the entries in joe’s (user 22) shopping cart: “SELECT * FROM cartentries WHERE uid=22” Check the actual code in the demo for more complex queries…

22 Sample Code: main.php main.php: the main program
takes care of the session (session variable userid) Determines what the query is and takes appropriate action. Many actions defined in other PHP files that are included using require Generates the base HTML for the document (including a small “header”).

23 Database Code: db.php php functions that interact with the database. This file is always “required” by main.php. Creates connection to the mysql server. Functions login, product_list, show_cart, add_to_cart, remove_from_cart and some HTML generating functions.

24 add.php Called from main.php when user is adding an item to cart:
require(“add.php”) Takes care of the logic for adding an item to the shopping cart for current user. Makes sure item exists.

25 login.php Called from main.php when user is trying to log in:
require(“login.php”) Takes care of the logic for login process: Decides what to send back if valid/invalid login.

26 logout.php Called from main.php when user is trying to log out:
require(“logout.php”) Takes care of the logic for log out: Terminates the session.

27 plist.php Called from main.php when user wants to see a list of products: require(“plist.php”) Just calls product_list function provided by db.php

28 remove.php Called from main.php when user is trying to remove an item from shopping cart: require(“remove.php”) Gets user id (from session) and product id (from HTTP query) Calls remove_from_cart Sends back resulting cart as HTML.

29 search.php Called from main.php when user is trying to search for products: require(“search.php”) If a search query is found in the HTTP query, processes the search. If no search query found, sends back a form that can be used to submit a search.

30 show.php Called from main.php when user is trying to see their shopping cart in: require(“show.php”) Just calls show_cart function found in db.php


Download ppt "PHP and MySQL."

Similar presentations


Ads by Google