100 likes | 306 Views
PHP and MySQL. PHP for the Web, page 333-378. 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”
E N D
PHP and MySQL PHP for the Web, page 333-378
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?”
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
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.
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.
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.
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
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