110 likes | 212 Views
PHP : Working with Resultsets. Last class. Open a connection to the MySQL server. Specify the database Issue queries (no updates at this stage) Display the results in the table form. Flush memory Close the connection. Today’s class. Learn to create database, tables, attributes using mySQL
E N D
Last class • Open a connection to the MySQL server. • Specify the database • Issue queries (no updates at this stage) • Display the results in the table form. • Flush memory • Close the connection.
Today’s class • Learn to create database, tables, attributes using mySQL • Embed PHP “type” code with HTML “type” code • Help and kick start your project in your SAD class • Help and kick start your project in this class • Good Luck coz you need LOTS OF IT!
Create Table • If you need help on data type definition • http://dev.mysql.com/doc/refman/5.0/en/data-types.html
Obtaining the number of columns/attributes • In MySQL you would do the following: $query = "SELECT * FROM employee"; $result = mysql_query($query); // need this to get resultSET if (mysql_error()) { die("cannot processed select query"); } $num = mysql_num_rows($result); // obtain the number of rows/rec $numFields = mysql_num_fields($result); //obtain the number of cols • To obtain the column/attribute names $name = mysql_field_name($result, $i); where $i is the column number .. going from 0 to ($numFields – 1)
Obtaining the number of columns/attributes • Obtaining the length of an attribute $attrLen = mysql_field_len($result, $i); where $i is the column number .. going from 0 to ($numFields – 1) • Obtaining the attribute type $attrType = mysql_field_type($result, $i); where $i is the column number .. going from 0 to ($numFields – 1)
More function • Obtain the tables within the database $tables = mysql_list_tables("test_alum"); $numTables = mysql_num_rows($tables); $tableName = mysql_tablename($tables, $k); will give you the table names where $k goes from 0 to ($numTables – 1)
More function – field names $fields = mysql_list_fields("test_alum", $tableName); // field result $numfields = mysql_num_fields($fields); //get the number of fields // Do a for loop here to get the attribute name for index 0 to ($numfields – 1) for (…. ){ $fieldInfo = mysql_fetch_field($fields); echo $fieldInfo->name; } Note: The mysql_fetch_field() command will get you the NEXT attribute property.
Other characteristics echo $fieldInfo->blob; echo $fieldInfo->max_length; echo $fieldInfo->not_null; echo $fieldInfo->numeric; echo $fieldInfo->primary_key; echo $fieldInfo->table; echo $fieldInfo->type; echo $fieldInfo->unique_key; echo $fieldInfo->unsigned; echo $fieldInfo->zerofill; echo $fieldInfo->multiple_key;
Exercises jex7.php, jex8.php combine with HTML coding. Use skeleton code • jex7.php: You are to show all the tables and for the test_alum database • jex8.php: You are to show the attributes for the “users” table. In particular, you have to show • the name of attribute; • the type of attribute; • whether it is numeric (show T is it is numeric and F if it is not)
Skeleton code <HTML> <HEAD> <TITLE>PHP CODE with HTML commands</TITLE> </HEAD> <BODY> <IMG src="mu.gif"> <BR> <BR> <?php //insert your PHP code inside here ?> </BODY> </HTML>