PHP and My SQL PHP for the Web
PHP and My. SQL PHP for the Web, page 333 -378
PHP and My. SQL Resource http: //dev. mysql. com/doc/refman/5. 5/en/ PHP – My. SQL Resource http: //us. php. net/manual/en/book. mysql. php http: //us. php. net/manual/en/mysql. examples-basic. php • Google “My. SQL Create table” • Google “PHP My. SQL how to connect? ”
My. SQL • 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 My. SQL server – Connect to a server – Query a server – Process query results
PHP: How to connect to a My. SQL 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.
Resources in PHP
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
PHP: How to get query results
- Slides: 10