Download presentation
Presentation is loading. Please wait.
1
PHP and MySQL PHP for the Web, page 333-378
2
PHP and MySQL MySQL Resource http://dev.mysql.com/doc/refman/5.5/en/ PHP – MySQL Resource http://us.php.net/manual/en/book.mysql.php http://us.php.net/manual/en/mysql.examples-basic.php Google “MySQL Create table” Google “PHP MySQL how to connect?”
3
MySQL Database server that is “almost free” Integrated into Apache Web Server and PHP PHP has many functions that can be used to interact with a MySQL server – Connect to a server – Query a server – Process query results
4
PHP: How to connect to a MySQL Server $link = mysql_connect( server, user, password); mysql_select_db(database_name, $link); mysql_connect return a “resource” A resource is a special variable type, holding a reference to an external resource.
5
Resources in PHP
6
PHP: How to run a query $link = mysql_connect( server, user, password); mysql_select_db(database, $link); $result = mysql_query(“SELECT * FROM table”); mysql_query returns a “resource” In this case $result is a link to a mysql resource.
7
PHP: mysql_fetch_array $q = “SELECT fname, lname FROM people WHERE userid = 65”; $result = mysql_query($q); $row = mysql_fetch_array($result); echo $row[‘fname’]. “ ”. $row[‘lname’]; Typically a query returns a table (a collection of rows). This query return one row.
8
PHP: mysql_fetch_array $q = “SELECT fname, lname FROM people”; $result = mysql_query($q); $row = mysql_fetch_array($result); echo $row[‘fname’]. “ ”. $row[‘lname’]; This query returns more than one row. We only print the fname and lname of the first returned row
9
PHP: mysql_fetch_array $q = “SELECT fname, lname FROM people”; $result = mysql_query($q); while ( $row = mysql_fetch_array($result) ) echo $row[‘fname’]. “ ”. $row[‘lname’]; mysql_fetch_array return null when it reaches the end of query results PHP interprets null as false
10
PHP: How to get query results
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.