Download presentation
Presentation is loading. Please wait.
1
Assignment help PHP + MySQL crash course
2019/5/14 Assignment help PHP + MySQL crash course Minpeng Zhu
2
Assignment Implement a web application for movie management with movie database as data storage and a web front-end using the Linux-Apache-MySQL-PHP, or LAMP, Web development framework To be completed in groups (3-5) Deploy solution See TA in the lab to get a MySQL account for your group Demo your solution during the scheduled labs or send solution (URL + source) by to TAs by 2019/5/14 Gyozo Gidofalvi
3
Connecting to MySQL from PHP
$hostname = "linne.it.uu.se"; $username = "…"; $password = "…"; $link = mysql_connect($hostname, $username, $password) or die("Could not open connection to database"); ?> Now the variable $link contains the information for your connection to MySQL. You can disconnect using: mysql_close($link) or die("Could not close connection to database"); 2019/5/14 Gyozo Gidofalvi
4
Select a database, issue queries
Once you have connected successfully, you should select the database you will use. Then you can issue queries to the database. <?php mysql_select_db(“your DB name", $link) or die("Could not select database"); $result = mysql_query("select * from some_table") or die("Could not issue MySQL query"); ?> Now the variable $result holds the query resultset. No ; at the end 2019/5/14 Gyozo Gidofalvi
5
Printing the results from a query
After issuing a query, you retrieve the results using the variable $result. Mysql_fetch_row function fetches each record from resultset in $result. <?php $result = mysql_query("select * from book_table") if (mysql_num_rows($result) == 0) { print("No results matching your query<BR>\n"); } else { print("Here are the results:<BR>\n"); //Fetching one row with mysql_fetch_row() while ($row = mysql_fetch_row($result)) { print "<td>".("$row[1] ")."</td>"; print "<td>".("$row[2] ")."</td>"; print "<td>".("$row[3] ")."</td>"; print "<td>".("$row[4] ")."</td>"; … print("</tr>"); } ?> 2019/5/14 Gyozo Gidofalvi
6
Printing the results from a query
Alternative, it is typically also each() and list() are used together to traverse an array. <?php $result = mysql_query("select * from book_table") if (mysql_num_rows($result) == 0) { print("No results matching your query<BR>\n"); } else { print("Here are the results:<BR>\n"); //Fetching one row with mysql_fetch_row() while ($row = mysql_fetch_row($result)) { while (list($key, $value) = each($row)) { print("$value "); } print("<BR>\n"); ?> 2019/5/14 Gyozo Gidofalvi
7
The above example would be used to show book data from a book table in a following way. e.g,
BookName Publish year Price Rating Database 2000 500 5 2019/5/14 Gyozo Gidofalvi
8
HTML forms and PHP variables
From an HTML page that had this form: <FORM ACTION="target.php" METHOD="GET"> <INPUT TYPE="TEXT" NAME="myvar"> <INPUT TYPE="TEXT" NAME="yourvar"> <INPUT TYPE="SUBMIT" NAME="submit"> </FORM> The PHP script (target.php) will receive the variables value from the form by the same name: <?php $myvar = $_GET[myvar]; $yourvar = $_GET[yourvar] print("you entered $myvar and $yourvar"); ?> 2019/5/14 Gyozo Gidofalvi
9
Drop down list usage Example of filling drop down list value from a database table. <tr><td>Category</td> <td> <select name=“Category"> <?php $result = mysql_query("select name from category_table"); while($row = mysql_fetch_row($result)) { print("<option>$row[0]</option>"); } ?> </select> </td></tr> 2019/5/14 Gyozo Gidofalvi
10
Technique reference http://www.w3schools.com/
2019/5/14 Gyozo Gidofalvi
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.