sql_in

The sql_in() function is used to create a column condition using the IN SQL keyword.

It employs 5 parameters:

  1. $val is the name of the column,
  2. $valeurs is the list of values, in the form of an array or a comma-separated sequence of strings. These values will be automatically filtered using sql_quote,
  3. $not is used to provide negation. By default it is empty ''; assign 'NOT' to execute a NOT IN condition,
  4. $serveur,
  5. $option.

It can be used as follows:

$vals = array(2, 5, 8);
// where $vals = "2, 5, 8";
$in = sql_in('id_table', $vals);
if ($res = sql_select('column', 'table', $in)) {
	// ...
}

Example

The "Tickets" plugin uses sql_in() to obtain the title of a ticket only if it has a status matching one of those listed:

function inc_ticket_forum_extraire_titre_dist($id_ticket){
	$titre = sql_getfetsel('titre', 'spip_tickets', array(
		'id_ticket = ' . sql_quote($id_ticket),
		sql_in('statut', array('ouvert', 'resolu', 'ferme'))
	));
	return $titre;
}

Author Mark Baber Published : Updated : 12/03/23

Translations : English, français