Upload
randell-lester
View
217
Download
2
Embed Size (px)
Citation preview
MySQL MySQL Databases & Databases & PHP IntegrationPHP Integration
Using PHP to write data to, and retrieve Using PHP to write data to, and retrieve data from, a MySQL databasedata from, a MySQL database
Interacting with Interacting with DatabasesDatabases
We used XML pages statically to act as databasesWe used XML pages statically to act as databases
Relational Database:Relational Database:
Think of an Excel document where each Think of an Excel document where each worksheet is a table within the document worksheet is a table within the document (database)(database)
A table is rows / columns while a database is A table is rows / columns while a database is records / fieldsrecords / fields
Data is stored in database, not RAM as with XMLData is stored in database, not RAM as with XML
MySQL Command LineMySQL Command Line
Login: mysql –u Login: mysql –u usernameusername –p –p(prompt for password)(prompt for password)
SHOW DATABASES, CREATE, ALTER, DROP, SHOW DATABASES, CREATE, ALTER, DROP, SELECT, INSERT, UPDATE, DELETESELECT, INSERT, UPDATE, DELETE
Must use commands (in all caps) and Must use commands (in all caps) and semicolons (;) at the end to execute stringssemicolons (;) at the end to execute strings
phpMyAdminphpMyAdmin
Web-based database management system Web-based database management system written in PHPwritten in PHP
Used to manipulate any database item, view Used to manipulate any database item, view items, perform queries, etc…items, perform queries, etc…
Table Creation:Table Creation:
Field Name, Type (usually VARCHAR), Length Field Name, Type (usually VARCHAR), Length Values, Collation, Attributes, Null, Default, ExtraValues, Collation, Attributes, Null, Default, Extra
Login using your username (flastname) and Login using your username (flastname) and password (password)password (password)
Databases and PHPDatabases and PHP
PHP function: “mysqli_connect” / PHP function: “mysqli_connect” / “mysqli_close”“mysqli_close”
Test our connection to our database using Test our connection to our database using PHPPHP
<?php<?php$dbconnect = mysqli_connect(“192.168.254.124”, “flastname”, $dbconnect = mysqli_connect(“192.168.254.124”, “flastname”,
“password”);“password”);echo “<p>MySQL Client Version: “, mysqli_get_client_info(), “</p>”;echo “<p>MySQL Client Version: “, mysqli_get_client_info(), “</p>”;echo “<p>MySQL Connection: “, mysqli_get_host_info($dbconnect), echo “<p>MySQL Connection: “, mysqli_get_host_info($dbconnect),
“</p>”;“</p>”;echo “<p>MySQL Protocol Version: “, echo “<p>MySQL Protocol Version: “,
mysqli_get_proto_info($dbconnect), “</p>”;mysqli_get_proto_info($dbconnect), “</p>”;echo “<p>MySQL Server Version: “, echo “<p>MySQL Server Version: “,
mysqli_get_server_info($dbconnect), “</p>”;mysqli_get_server_info($dbconnect), “</p>”;mysqli_close($dbconnect);mysqli_close($dbconnect);
?>?>
sample1.html & sample1.html & sample1.phpsample1.php
sample1.html – grabs user input in an HTML sample1.html – grabs user input in an HTML form and holds it in memoryform and holds it in memory
sample1.php – connects to a MySQL database sample1.php – connects to a MySQL database for the insertion of the data to the tablefor the insertion of the data to the table
http://192.168.254.124/~instructor/CIS115-http://192.168.254.124/~instructor/CIS115-Spring2011/Lecture5/Spring2011/Lecture5/
View sample1.html and sample1.php’s codeView sample1.html and sample1.php’s code
View phpMyAdmin to see the customer View phpMyAdmin to see the customer database – sample1 table recordsdatabase – sample1 table records
dbexample1.html & dbexample1.html & dbexample1.phpdbexample1.php
dbexample1.html – provides a username/password dbexample1.html – provides a username/password field for logging into the databasefield for logging into the database
dbexample1.php– connects to a MySQL database dbexample1.php– connects to a MySQL database for the selection and viewing of table data in an for the selection and viewing of table data in an organized tableorganized table
http://192.168.254.124/~instructor/CIS115-http://192.168.254.124/~instructor/CIS115-Spring2011/Lecture5/Spring2011/Lecture5/
View dbexample1.html and dbexample1.php’s View dbexample1.html and dbexample1.php’s codecode
View phpMyAdmin to see the customer database View phpMyAdmin to see the customer database – sample1 table records– sample1 table records
order.html & order.phporder.html & order.php
order.html gathers data and sends it to order.html gathers data and sends it to order.php for verificationorder.php for verification
Nothing is written to the database…data is just Nothing is written to the database…data is just reprinted on the screenreprinted on the screen
http://192.168.254.124/~instructor/CIS115-http://192.168.254.124/~instructor/CIS115-Spring2011/Lecture5/orders/Spring2011/Lecture5/orders/
orderdb.html & orderdb.html & orderdb.phporderdb.php
orderdb.html gathers data and sends it to orderdb.html gathers data and sends it to orderdb.php for verificationorderdb.php for verification
orderdb.php inputs that data into a database for orderdb.php inputs that data into a database for storage using fields corresponding to the form storage using fields corresponding to the form variablesvariables
http://192.168.254.124/~instructor/CIS115-http://192.168.254.124/~instructor/CIS115-Spring2011/Lecture5/orders/Spring2011/Lecture5/orders/
orderdboutput.html & orderdboutput.html & orderdboutput.phporderdboutput.php
orderdboutput.html provides a login form for a orderdboutput.html provides a login form for a user to log in and view the contents of the user to log in and view the contents of the database tabledatabase table
orderdboutput.php grabs the entered orderdboutput.php grabs the entered username/password, checks it against the username/password, checks it against the databse, then displays the table records in an databse, then displays the table records in an organized HTML tableorganized HTML table
http://192.168.254.124/~instructor/CIS115-http://192.168.254.124/~instructor/CIS115-Spring2011/Lecture5/orders/Spring2011/Lecture5/orders/