Check if “is digits” and does not begin with zero

Protecting Your PHP/SQL Queries from SQL Injection is one of coding obligations, if you have tables with AUTO_INCREMENT and you want to check IDs before running queries, this function is yours.

function is_digits($string){
	return preg_match("!^[1-9]([0-9]+)?$!",$string);
}

Examples :

is_digits(0); // Return 0
is_digits(1); // Return 1
is_digits("1"); // Return 1
is_digits(01); // Return 0
is_digits(-1); // Return 0
is_digits("1a"); // Return 0
is_digits("a1"); // Return 0
is_digits(15); // Return 1
is_digits(1,5); // Return 0
is_digits(1.5); // Return 0

Mysql FIND_IN_SET()

FIND_IN_SET() : A function that I discovered some years ago and which I find very impressive if you’re addicted to query optimization.
This function is a bit like in_array() but is doing research in StringList separated by commas “,”

Here is an example to understand:

Table : Clubs
table_clubs

Table : Leagues
championnats

To select leagues which participates BARCA (having id 11), SQL code would look like this :

SELECT    league_id,name
FROM      Leagues
WHERE     FIND_IN_SET(11, clubs_id)