MySQL: Get total number of rows when using LIMIT

A SELECT statement may include a LIMIT clause to restrict the number of rows return by the MySQL server. In some cases, it is desirable to know how many rows the SELECT statement would have returned without the LIMIT. To obtain the row count by include SQL_CALC_FOUND_ROWS option in the SELECT statement, and then execute FOUND_ROWS().
[sql]

SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name LIMIT 10;

SELECT FOUND_ROWS();

[/sql]
The second SELECT returns a number indicating how many rows the first SELECT query would have returned without the LIMIT clause. i.e, it returns the total count.

In the absence of the SQL_CALC_FOUND_ROWS option in first SELECT, FOUND_ROWS() returns the number of rows in the result set returned by that statement i.e, it returns only 10.

Permanent link to this article: https://blog.openshell.in/2012/08/mysql-get-total-number-of-rows-when-using-limit/

Pass command line argument to PHP

The command line arguments are stored in an array in the $_SERVER variables called ‘argv’. Running the command “php index.php test1 test2” and then doing print_r($_SERVER[‘argv’]) would output this:
[php]
Array
(
[0] => index.php
[1] => test1
[2] => test2
)
[/php]
You can also use $argv, It has return the same value. $argc variable to find the count of parameters passing in the command line argument.

Sample code used to pass argument form command line
[php]
<?php

if (isset($_SERVER[‘argc’])) {
if ($argc != 5)
die("Usage: index.php <username> <password> <receiver> <message>\n");

$username = $argv[1];
$password = $argv[2];
$receiver = $argv[3];
$message = $argv[4];
}

?>
[/php]

run the application

php index.php username password receiverno “TEST MSG FROM USER”

Permanent link to this article: https://blog.openshell.in/2012/07/pass-command-line-argument-to-php/

Segmentation fault with rake db:create in ruby on rails

Whenever we invoke rake db:migrate, getting segmentation fault error.

Segmentation fault
ruby 1.9.1p430 (2010-08-16 revision 28998) [i386-mingw32]

Code Aborted

probably a bug in our database connectivity DLL file

Place this linmysql.dll file within your ruby/bin folder. If the file exists already, replace that file by this file.

Download
libmySQL.dll

Permanent link to this article: https://blog.openshell.in/2012/07/segmentation-fault-with-rake-dbcreate-in-ruby-on-rails/

YII Primary Key error

Error: Table “user” does not have a primary key.

Solution:
The crud command requires primary key to be defined in your tables. If you are working with legacy tables and can’t specify primary key, you can declare it in primaryKey() method of your model class.

override method primaryKey in protected/models/User.php
[php]
public function primaryKey()
{
return ‘id’;
}

[/php]

Permanent link to this article: https://blog.openshell.in/2012/04/yii-primary-key-error/

APC extension Enable in apache

To enable APC extension on Ubuntu, Use the following command

sudo apt-get install php-apc

To enalbe APC extension on Windows,

1) Go to http://downloads.php.net/pierre/
2) Download php_apc-.zip
3) Copy php_apc.dll to /ext folder
4) In your php.ini, make sure you have: extension=php_apc.dll
5) Restart your server.

Permanent link to this article: https://blog.openshell.in/2012/04/apc-extension-enable-in-apache/

How To Install memcached with memcache PHP Extension on Ubuntu

memcached and the PHP5 memcache module are available as packages, so we can install them as follows:

sudo apt-get install memcached php5-memcache

Permanent link to this article: https://blog.openshell.in/2012/04/how-to-install-memcached-with-memcache-php-extension-on-ubuntu/

Enable Sqlite in PHP on Apache

To enable SQLite on Windows/Apache/PHP setup, Uncomment the following lines in the php.ini file and restart Apache:

[code]
extension=php_pdo.dll
extension=php_pdo_sqlite.dll
[/code]

To enable SQLite on Ubuntu, Use the following command
[code]
sudo apt-get install sqlite php5-sqlite
sudo apt-get install php-pear php-apc php5-curl
[/code]

Permanent link to this article: https://blog.openshell.in/2012/04/enable-sqlite-in-php-on-apache/

Java Essential Tips: How to set null value in Java JDBC PreparedStatement

Target Audience: Java Beginners, JDBC Developers.

What should you know already?: JDBC, PreparedStatement, SQL queries

The Java beginners who works on JDBC should have understanding of what the table and its structure is. It would help to deal with parameters, especially mapping ‘null’ values. Because ‘null’ability of a field should be handled carefully when you pass through Java program.

Passing ‘null’ to String is different from passing ‘null’ to int

Consider the code snippet, in which the SQL query expects 2 parameters: name and address of type VARCHAR.

[java]
String query="INSERT INTO person (name, address) VALUES (?,?)";
PreparedStatement ps=c.prepareStatement(query); // c – java.sql.Connection
ps.setString(1, "Ganesh");
ps.setString(2, null);
ps.executeUpdate();
[/java]

The above code works fine and inserts a row with ‘null’ in address column. Now check the following code, which contains one more column contact_number of type BIGINT. This column allows ‘null’. Here trying to insert a person whose contact number is not known ie null, neither be zero.

[java]
String query="INSERT INTO person (name, address, contact_number) VALUES (?,?,?)";
PreparedStatement ps=c.prepareStatement(query); // c – java.sql.Connection
ps.setString(1, "Raja Raman");
ps.setString(2, null);
ps.setLong(3, null); // error
ps.executeUpdate();
[/java]

The above code will give a error at line 5. Because the method setLong expects a value of primitive date type long, but here it is ‘null’ type.

How to pass ‘null’ to number column

The correct way to pass ‘null’ to int or long or any number type could be as follows:

[java]
String query="INSERT INTO person (name, address, contact_number) VALUES (?,?,?)";
PreparedStatement ps=c.prepareStatement(query); // c – java.sql.Connection
ps.setString(1, "Raja Raman");
ps.setString(2, null);
ps.setNull(3, java.sql.Types.BIGINT); // no error, perfect
ps.executeUpdate();
[/java]

The setNull method gives a flexible way of passing ‘null’ value to almost all kind of data types available in a typical relational database.

Feel free to comment. Happy Programming.

Permanent link to this article: https://blog.openshell.in/2012/03/java-essential-tips-how-to-set-null-value-in-java-jdbc-preparedstatement/

Java Essential Tips: Static Import

Static Import

Static import is a mechanism to include constants into your code without referring its class in which the constants defined. For example, if you are using Integer.MAX_VALUE or some other constants field of class java.lang.Integer then you could simply use the following static import statement:

[java]import static java.lang.Integer.*;
class Demo{
public static void main(String [] a){
System.out.println(MAX_VALUE+MIN_VALUE);
}
}[/java]

Note that the normal import has the following syntax:
[java]import packagename.*;
import packagename.classname;[/java]
whereas, static import has the following syntax:
[java]import static packagename.classname.*[/java]
For example,
[java]import static java.lang.Math.*[/java]
If you use the above static import in your class, then you could use PI and E constants without referring its class name ie. Math.PI, check the following code:
[java]System.out.println(PI);[/java]

Permanent link to this article: https://blog.openshell.in/2012/01/java-essential-tips-static-import/

Custom Date Validation in ASP.Net

The validators shipped with .NET are great tools to check and validate what the user enters on a web form, before processing it. With the DateValidator control this gap has been filled. User can insert date in a common text box and that’s all: you won’t have to bother about validating their input, the only request is that the date must be in the format dd/mm/yyyy or user defined format.

Let us get in to the code.

A RangeValidatorwould work here to check the date is greater than today:

<asp:RangeValidator ID=”RangeValidator1″ runat=”server” ControlToValidate=”txt_start_date” Display=”None” ErrorMessage=”Date must be greater than today” Type=”Date”></asp:RangeValidator>

<cc1:ValidatorCalloutExtender ID=”ValidatorCalloutExtender4″ runat=”server” Enabled=”True”                                                       TargetControlID=”RangeValidator1″></cc1:ValidatorCalloutExtender>

Code Behind:

  RangeValidator1.MinimumValue = DateTime.Now.Date.ToString(“dd/MM/yyyy”);
RangeValidator1.MaximumValue = System.DateTime.MaxValue.ToString(“dd/MM/yyyy”);

If you want to do comparison between two dates we can use  compare validator:

<asp:CompareValidator ID=”CompareValidator5″ runat=”server” Operator=”GreaterThan”   Type=”Date” Display=”None” ErrorMessage=”EndDate must be greater than StartDate”   ControlToValidate=”txt_end_date” ControlToCompare=”txt_start_date”></asp:CompareValidator>
<cc1:ValidatorCalloutExtender ID=”ValidatorCalloutExtender5″ runat=”server” Enabled=”True” TargetControlID=”CompareValidator5″> </cc1:ValidatorCalloutExtender>

You can use any (Operator=”GreaterThan” ) like LessThan,Equal ,etc.,

I attached some pieces of image for the reference of output:

Note:

If  it is not working add the below line in web.config file under <system.web>

<globalization requestEncoding=”utf-8″ responseEncoding=”utf-8″ culture=”en-GB” uiCulture=”en”/>

Happy Coding….

Permanent link to this article: https://blog.openshell.in/2011/10/custom-date-validation-in-asp-net/