220 likes | 322 Vues
Learn PHP string manipulation, database interaction, and Agile development in this session. Covering string functions, MySQL connection, query execution, data fetching, and Agile concepts. Join Solvith's PHP course with James Song.
E N D
Programming Methodology and Web Rapid Prototyping (Session 5) TC101 , 5 Sessions course, Conducted by Solvith http://solvith.com/ACJC Solvith PHP Course- James Song (81273798)
Lesson Objectives Session 5 [24 Aug] • String Basics (30 mins) • Database (Brief) (30 mins) • Agile Development (1 Hour) • Test (1 Hour) • Project Presentation(1 Hour) • Break And Closing Solvith PHP Course- James Song (81273798)
PHP Strings A string is series of characters. In PHP, a character is the same as a byte, which is exactly 256 different characters possible. <?php $s=“I am a string”; $s2=‘I am also a string”; print $s.”---”.$s2; ?> GeshanManandhar.com
PHP Strings Another Example <?php $beer = 'Heineken'; echo "<br>$beer's taste is great."; // works, "'" is an invalid character for varnames echo "<br>He drank some $beers."; // won't work, 's' is a valid character for varnames echo "<br>He drank some ${beer}s."; // works echo "<br>He drank some {$beer}s."; // works ?> GeshanManandhar.com
Important String Functions • explode (string $delimiter, string $string) • nl2br ( string $string ) • strcmp ( string $str1, string $str2 ) • strlen ( string $string ) • strtolower ( string $str ) • substr ( string $string, int $start [, int $length] ) • trim ( string $str) GeshanManandhar.com
PHP and Databases • PHP supports about 20 RDBM servers • Including MySQL, Oracle, MS SQL, DB2, Firebird and Paradox • Supports connection over ODBC driver • Provided different sets of functions for accessing the different RDBMS • Each function starts with prefix – the DB server typeExample: mysql_connect, mssql_query, etc
Connecting MySQL • mysql_connect – function to connect to MySQL server • Parameters: $server, $username, $password, $new_link, $client_flags • Returns resource result, identifying the new link (link identifier) • The result is used as parameter to other mysql_ functions mysql_connect("localhost", "root", "rootpass");
Connecting MySQL (2) • Once connected a database must be selected to perform queries upon • In some cases it is not required – show databases query for instance • mysql_select_db ($dbname, $link) – selects database from the server • Returns true if successful $dblink = mysql_connect("local host", "root", "rootpass"); mysql_select_db("mydb", $dblink);
Executing Query • mysql_query ($query, $link) – execute query on database • $query is string – the query to be executed • $link is database link identifier • The returned result depends on the query • If query is select, show, describe, explain – returns resource or false on error • Otherwise true if successful, false on error • The link parameter can be omitted in all mysql_ functions if working with only one database • Only one call to msql_connect in the script mysql_query("select * from users", $dblink);
Select Query Results • PHP provides several functions for working with MySQL select query results • mysql_query returns resource when performing select query that holds the data • The result is accessed row-per-row from first towards last with internal pointer • Additional functions to get number of affected rows on update/delete or auto-generated id of inserted row
Fetch Row From Result • mysql_fetch_row – returns numerical array, containing the current row from the result and moves the pointer to the next row • Returns false if there are no more rows $res = mysql_query ("select id, name from people"); $row = mysql_fetch_row($res); if ($row) print_r($row); // 0->id, 1->name else echo "No results!";
Fetching Row From Result (2) • mysql_fetch_assoc – returns associative array containing the current row in result and moved the pointer to the next one • The field names are keys in the array • Returns false if no more rows $res = mysql_query ("select id, name from people"); $row = mysql_fetch_assoc($res); if ($row) echo "Name: ".$row['name'];
Fetching Single Value • mysql_result ($result, $row, $field) – return the value or single cell In MySQL query result • $field is either field index or name • Returns false on failure • Must NOT be mixed with other functions for reading query result • Much slower than fetching data row-per-row $res = mysql_query ("select count(*) from people"); echo mysql_result($res, 0, 0);
Number of Rows • mysql_num_rows ($result) – returns the number of rows in the result set • Does not work with unbuffered queries (mysql_unbuffered_query) $res = mysql_query ("select id, name from people"); $count = mysql_num_rows($res); echo $count;
What Is Agile • Agile --readiness for motion, nimbleness, activity, dexterity in motion • Agility The ability to both create and respond to change in order to profit in a turbulent business environment • Companies need to determine the amount of agility they need to be competitive • Chaordic • Exhibiting properties of both chaos and order • The blend of chaos and order inherent in the external environment and in people themselves, argues against the prevailing wisdom about predictability and planning • Things get done because people adapt, not because they slavishly follow processes
Agile Software Development • Agile software development is a conceptual framework for software engineering that promotes development iterations throughout the life-cycle of the project. • Software developed during one unit of time is referred to as an iteration, which may last from one to four weeks. • Agile methods also emphasize working software as the primary measure of progress
Agile Software Development: Intro • Characteristics of Agile Software Development -- Light Weighted methodology -- Small to medium sized teams -- vague and/or changing requirements -- vague and/or changing techniques -- Simple design -- Minimal system into production
Agile Software Development: Intro • Do not reinvent the wheel • Use open-source if possible • Prototype first. Optimize Later • Change if need to during development