Posts Mentioning RSS Toggle Comment Threads | Keyboard Shortcuts

  • ashish revar 1:47 pm on February 27, 2009 Permalink | Reply  

    php: array length 

     

    PHP array length or size count


    Array is very flexible and any time we can add element to array  or remove element from an array. So we must know what are the present number of elements in an array. To know the total length or total element in an array we have to use count command. 

    We will first create an array like this 

    $array = array  (“ABC”,”DEF”, “GHI”, “KLM”); 

    //Now we will use the count command to know the number of elements in this array. 

    print count($array);     

    //This will output 4 as there are four elements inside the array. 

    You can use sizeof function also to determine the number of values 

    $value= array(2,5,6,8,9);
    echo “size of array = “.sizeof($value).”<br>”; // Output = 5 

    The above line output will be 5 

     
  • ashish revar 1:46 pm on February 27, 2009 Permalink | Reply  

    php: array count 

    count

    (PHP 4, PHP 5)

    count — Count all elements in an array, or properties in an object

    Description

    int count ( mixed $var [, int $mode= COUNT_NORMAL ] )

    Counts all elements in an array, or properties in an object.

    For objects, if you have SPL installed, you can hook into count() by implementing interface Countable. The interface has exactly one method, count(), which returns the return value for the count() function.

    Please see the Array section of the manual for a detailed explanation of how arrays are implemented and used in PHP.

    Parameters

     

    var

    The array.

    mode

    If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array. The default value for mode is 0.count() does not detect infinite recursion.

     

    Return Values

    Returns the number of elements in var , which is typically an array, since anything else will have one element.

    If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var isNULL0 will be returned.

    Caution

    count() may return 0 for a variable that isn’t set, but it may also return 0 for a variable that has been initialized with an empty array. Use isset() to test if a variable is set.

    Changelog

     

    Version Description
    4.2.0 The optional mode parameter was added.

     

    Examples

     

    Example #1 count() example

    <?php
    $a
    [0] = 1;
    $a[1] = 3;
    $a[2] = 5;
    $result count($a);
    // $result == 3

    $b[0]  = 7;
    $b[5]  = 9;
    $b[10] = 11;
    $result count($b);
    // $result == 3

    $result count(null);
    // $result == 0

    $result count(false);
    // $result == 1
    ?>

     

     

    Example #2 Recursive count() example

    <?php
    $food 
    = array('fruits' => array('orange''banana''apple'),
                  
    'veggie' => array('carrot''collard''pea'));

    // recursive count
    echo count($foodCOUNT_RECURSIVE); // output 8

    // normal count
    echo count($food); // output 2

    ?>

     
  • ashish revar 1:40 pm on February 27, 2009 Permalink | Reply  

    php: mysql_query 

    mysql_query

    (PHP 4, PHP 5)

    mysql_query — Send a MySQL query

    Description

    resource mysql_query ( string $query [, resource $link_identifier ] )

    mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that’s associated with the specified link_identifier .

    Parameters

     

    query

    A SQL query

    The query string should not end with a semicolon.

    link_identifier

    The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level error is generated.

     

    Return Values

    For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resourceon success, or FALSE on error.

    For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success orFALSE on error.

    The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

    Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

    mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

    Examples

     

    Example #1 Invalid Query

    The following query is syntactically invalid, so mysql_query() fails and returns FALSE.

    <?php
    $result 
    mysql_query('SELECT * WHERE 1=1');
    if (!
    $result) {
        die(
    'Invalid query: ' mysql_error());
    }

    ?>

     

     

    Example #2 Valid Query

    The following query is valid, so mysql_query() returns a resource.

    <?php
    // This could be supplied by a user, for example
    $firstname 'fred';
    $lastname  'fox';

    // Formulate Query
    // This is the best way to perform a SQL query
    // For more examples, see mysql_real_escape_string()
    $query sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
        
    mysql_real_escape_string($firstname),
        
    mysql_real_escape_string($lastname));

    // Perform Query
    $result mysql_query($query);

    // Check result
    // This shows the actual query sent to MySQL, and the error. Useful for debugging.
    if (!$result) {
        
    $message  'Invalid query: ' mysql_error() . "\n";
        
    $message .= 'Whole query: ' $query;
        die(
    $message);
    }

    // Use result
    // Attempting to print $result won't allow access to information in the resource
    // One of the mysql result functions must be used
    // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
    while ($row mysql_fetch_assoc($result)) {
        echo 
    $row['firstname'];
        echo 
    $row['lastname'];
        echo 
    $row['address'];
        echo 
    $row['age'];
    }

    // Free the resources associated with the result set
    // This is done automatically at the end of the script
    mysql_free_result($result);
    ?>

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel