PHP II - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

PHP II

Description:

Available in PHP 4. easy way to iterate over arrays ... Any valid PHP code may appear inside a function, even other functions and class definitions ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 30
Provided by: diglib1Ce
Category:
Tags: php | php

less

Transcript and Presenter's Notes

Title: PHP II


1
PHP - II
  • Control Structures
  • Functions
  • MySQL functions

2
Control Structures
  • if (expr) statement
  • if (expr) statements
  • if (expr) statements else statements
  • if (expr) statements
  • elseif (expr) statements
  • else statements

3
Control Structures Continued
  • while (expr)
  • do while (expr)
  • for (expr1expr2expr3)
  • foreach (array_expr as value)
  • foreach (array_expr as keyvalue)
  • switch() case 0 statements
  • case 1 statements
  • break, continue

4
foreach on arrays
  • Available in PHP 4
  • easy way to iterate over arrays
  • Generates error when attempted with other
    variables
  • Two syntax available

5
foreach Syntax 1
  • foreach (array_expression as value) statement
  • Given arr contains 1, 2, 3, 4, 6, 12 values
  • foreach (arr as val)
  • echo val,
  • Will print
  • 1, 2, 3, 4, 6, 12

6
foreach Syntax 2
  • foreach (array_expression as key value)
    statement
  • Given arr contains 1, 2, 3, 4, 6, 12 values
  • foreach (arr as key val)
  • echo key key is val, \n
  • Will print
  • ????

7
Break
  • Immediately end execution of currently executing
  • for
  • foreach
  • while
  • do while
  • switch
  • accepts an optional numeric argument which tells
    it how many nested enclosing structures are to be
    broken out of

8
Continue
  • used within looping structures to skip the rest
    of the current loop iteration and continue
    execution at the beginning of the next iteration
  • accepts an optional numeric argument which tells
    it how many levels of enclosing loops it should
    skip to the end of

9
Functions
  • Basic Function
  • arg_n)   echo "Example function.\n"  
    return retval?

10
Functions
  • Any valid PHP code may appear inside a function,
    even other functions and class definitions

11
Conditional Functions
  • function foo ()     echo "I don't exist until
    program execution reaches me.\n" 

12
Functions inside functions ?
  • echo "I don't exist until foo() is called.\n" 

13
Functions with optional args?
  • foo.bar
  • If bar is not specified, it will default

14
MySQL functionality
  • Two primary resources associated with MySQL
  • Link
  • Query Results

15
mysqli_connect 
  • resource mysqli_connect ( string host
  • , string username , string password
  • , string dbname
  • , int port , string socket)Returns a
    MySQL link identifier on success, or FALSE on
    failure.

16
mysqli_select_db
  • bool mysqli_select_db ( mysqli link, string
    database_name)Returns TRUE on success or FALSE
    on failure.
  • If you have multiple databases inside MySQL,
    this function can be used to switch between them.

17
mysql_list_tables
  • resource mysql_list_tables ( string database ,
    resource link_identifier)mysql_list_tables()
    takes a database name and returns a result
    pointer much like the mysql_query() function. Use
    the mysql_tablename() function to traverse this
    result pointer, or any function for result
    tables, such as mysql_fetch_array().

18
mysqli_query 
  • resource mysqli_query ( mysqli link,
  • string query , int resultMode)mysqli_quer
    y() sends a query to the currently active
    database on the server that's associated with the
    specified link identifier. If no link is open,
    the function tries to establish a link as if
    mysqli_connect() was called with no arguments,
    and use it. The result of the query is buffered.

19
mysqli_query 
  • Notes
  • The query string should not end with a semicolon.
  • SELECT, SHOW, DESCRIBE, EXPLAIN return resource
    (result object)
  • Other statements (INSERT, DELETE, UPDATE, etc) do
    NOT return resources.

20
mysqli_fetch_array
  • array mysqli_fetch_array ( resource result , int
    result_type)Returns an array that corresponds
    to the fetched row, or NULL if there are no more
    rows.

21
mysqli_fetch_array
  • Result Types
  • MYSQLI_ASSOC
  • Array indexed ONLY by column (attribute) names
  • Same as mysqli_fetch_assoc()
  • MYSQLI_NUM
  • Array indexed ONLY numerically, starting with 0
  • Same as mysqli_fetch_row()
  • MYSQLI_BOTH
  • Array indexed both numerically as well as by
    column names
  • Default for mysqli_fetch_array()

22
mysql_fetch_array
  • Using MYSQL_ASSOC, you only get associative
    indices
  • Same as mysql_fetch_assoc()
  • Using MYSQL_NUM, you only get number indices
  • Same mysql_fetch_row()

23
mysqli_free_result 
  • void mysqli_free_result ( resource result)
  • Will free all memory associated with the result
    identifier result.
  • mysqli_free_result() needs to be called if you
    are concerned about how much memory is being used
    for queries that return large result sets. All
    associated result memory is automatically freed
    at the end of the script's execution. However,
    not a bad idea to get in the habit of freeing
    memory anyway.

24
mysqli_close
  • bool mysql_close ( mysqli link)
  • Terminates an open database connection
  • Returns TRUE on success and FALSE on failure

25
Other useful functions
  • mysqli_num_rows()
  • Returns number of rows from SELECT
  • mysqli_affected_rows()
  • Returns number of rows affected by last INSERT,
    UPDATE, REPLACE, or DELETE
  • mysqli_error()
  • Returns a description of last error

26
Complete Example
  • mysqli_connect( "mysql_host",
    "mysql_user", "mysql_password,
    dbname)   or die("Could not connect " .
    mysqli_error())echo "Connected successfully"

27
Complete Example ... cont
  • / Performing SQL query /query "SELECT
    FROM my_table "result mysqli_query(link,
    query)
  • or die("Query failed " . mysqli_error())

28
Complete Example cont
  • / Printing results in HTML /echo
    "\n"while (line mysqli_fetch_array(re
    sult, MYSQLI_ASSOC))
  •    echo \n"   foreach (line as
    col_value)
  • echo col_value\n  
    echo \n"echo "\n"

29
Complete Example cont
  • / Free resultset /mysql_free_result(result)
    / Closing connection /mysql_close(link)
  • ?

30
More on mysqli_query()
  • mysqli_query() will also fail and return FALSE if
    you don't have permission to access the table(s)
    referenced by the query.

31
To Do
  • Experiment with PHP
  • Use the links of the Course Website for help
Write a Comment
User Comments (0)
About PowerShow.com