Friday, 23 January 2015

Concatenate Array elements for Human Reading - PHP

This Source code used to Concatenate the Array Elements.For Example: Array is to be Printed.. John,Mary and Ishmal
<?php

$array = array('John', 'Mary', 'Ishmal');
echo concatenate($array);

function concatenate($elements, $delimiter = ', ', $finalDelimiter = ' and ')
{
$lastElement = array_pop($elements);
return join($delimiter, $elements) . $finalDelimiter . $lastElement;
}
?>
 
O/P: John,Mary and Ishmal

Script running time - PHP




This code snippet is used to calculate the time taken by the script to execute..
<?php
$time_start = microtime(true);

// Place your PHP/HTML/JavaScript/CSS/Etc. Here

$time_end = microtime(true);
$time = $time_end - $time_start;
echo 'Script took '.$time.' seconds';
?>

Tuesday, 20 January 2015

intval Function in PHP

PHP intval function is used to get an integer value from the given input value. It returns the integer value on success and 0 on failure.

Syntax:


intval(var_name, base)

var_name - Variable name
base - Base conversion ( Default 10 )

Example:


<?php
echo intval(28)."<br />";
echo intval(28,8)."<br />";
echo intval(28.1)."<br />";
echo intval(+28)."<br />";
echo intval(-28)."<br />";
echo intval(0x1B)."<br />";
echo intval(1e30)."<br />";
?>

Result


28
28
28
28
-28
27
0

In the above example, the given input is converted to integer value.
Note: The Prefix "0x" represents the Hexadecimal values(Base 16).
   The Prefix "0" represents the Octal values(Base 8).

Static Variable in PHP

PHP Static variable is declared using a keyword "static". It remains in local scope but retains the value till the program execution completes.

Syntax:


static varname;

In the above syntax, static is a keyword and varname is the variable name.

Example:


<?php
function count()
{
static $count=0;
$count++;
return $count;
}
$val1=count();
$val2=count();
echo $val1;
echo $val2;
?>

Result


1
2

In the above example, the variable $count is declared as static variable. So, it won't be initialized each time when the function count() is called.

Global Variable in PHP

PHP Global variable is declared outside the function. It can also be declared inside the function with the keyword "GLOBAL". It can be accessed from any part in the program.

Syntax:


global varname;

In the above syntax, global is a keyword. varname is the variable name.

Example:


<?php
function doublevalue()
{
global $tmp;
$retval=7*$tmp;
return $retval;
}
$tmp=5;
$dispval=doublevalue();
echo $dispval;
?>

Result


35

In the above example, $tmp is declared as a global variable. So, it can be accessed outside the doublevalue() function, even if it is declared inside the function.

SQL_CALC_FOUND_ROWS and FOUND_ROWS with MySQL

Using SQL_CALC_FOUND_ROWS and FOUND_ROWS with MySQL

Hello,
for this second blog article I've decided to explain this neat little feature of MySQL:SQL_CALC_FOUND_ROWS and FOUND_ROWS().
This article is about MySQL only, it is likely that these keywords/functions exist in other SQL-based languages but I've only ever used them with MySQL.

What's the point, you ask?
When working with (my)SQL databases, you often find yourself using the LIMIT keyword, in order for example to limit the results of a large search. Let's study a particular case.
- You're working on the "employees" table, which contains, say 1000 data rows.
- You wish to display the list of employees in a particular department, 10 by 10.
You wish to display the amount of employees in this department.

What would you normally do?
Here's a rough example:
$result = mysql_query("SELECT * FROM employees WHERE department='sales' LIMIT 0,10 ");
$total = mysql_num_rows($result);
-> This is incorrect because $total will never exceed 10 due to the LIMIT clause.

You could also do this:
$result = mysql_query("SELECT COUNT(*) cnt FROM employees WHERE department='sales'");
$count = mysql_result($result, 0, "cnt");
$result = mysql_query("SELECT * FROM employees WHERE department='sales' LIMIT 0,10 ");
-> But this means you have to process two SQL queries.


So how do you fix this?
Using the SQL_CALC_FOUND_ROWS keyword in your SQL query will allow you to fetch the total amount of results without being limited by the LIMIT clause.
Here's how you use it:

$data_result = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM employees WHERE department='sales' LIMIT 0,10");
$count_result = mysql_query("SELECT FOUND_ROWS() cnt");
$employees_count = mysql_result($count_result, 0, "cnt");
echo "$employees_count employees found in the sales dept. Showing first 10: ";
...

As you can see, using the SQL_CALC_FOUND_ROWS keyword allows you to save the amount of results before applying the LIMIT clause; and this amount can be retrieved with a simple "SELECT FOUND_ROWS()" after the query is executed.

Monday, 19 January 2015

FOUND_ROWS in mysql

FOUND_ROWS( )

The FOUND_ROWS( ) function returns the multiple rows with no LIMIT clause.
This function has the form:
SELECT FOUND_ROWS()

Return Value

Returns multiple rows with no LIMIT clause.

Examples

The following example shows the basic use of this function:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
    -> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();