June 11, 2012: Connecting PHP to MySQL

Today were thought how to connect Php to MySql.
Syntax: mysql_connect(hostname, username, password)

We are also thought how to select database, execute query, check for affected rows, counting number of rows in a selection, fetching array all with php.
Syntax:
select- mysql_select_db("database",connection)
query- mysql_query(query,connection)
check affected rows (delect,update,insert)- mysql_affected_rows()
check number of rows (select)- mysql_num_rows(data)
Fetch array: mysql_fetch_array(data, array-type)

Array types:
1. default is MYSQL_BOTH
2. MYSQL_ASSOC (Display associative array only)
3. MYSQL_NUM (Display Numeric array only)

It was a good learning as we were able to write short php programs and play around with our sample databases.  I downloaded HeidiSQL as my SQL interface.  It works well with the WAMP Manager in our school's PC. I use Dreamweaver as an IDE in class. At home, I am using MAMP.

No comments:

Post a Comment