PHP ADODB RecordSet Filters

From CoderLearner
Jump to: navigation, search

PHP ADODB Filter Return Records

Filter all rows in a recordset before display can be very beneficial when you want to manipulate the content of each record.

Syntax ADODB

include 'adodb5/rsfilter.inc.php';

function all_caselower(&$array, $rs) {
	foreach ($array as $k => $v) {
		if ($k == 'first_name' || $k == 'last_name') {
			$array[$k] = strtolower($v);
		}
	}
}

$sql = "SELECT id,first_name,last_name,email FROM {$table} WHERE email IS NOT NULL";
$conn1->SetFetchMode(ADODB_FETCH_BOTH);
$results = $conn1->Execute($sql);
$results = RSFilter($results, 'all_caselower');

Example 1

<?php

include 'adodb5/adodb.inc.php';

include 'adodb5/rsfilter.inc.php';

$host = 'localhost';
$user = 'user2000';
$pass = 'password2000';
$dbname = 'w3cyberlearning';

$conn1 = &ADONewConnection('mysql');
$conn1->PConnect($host, $user, $pass, $dbname);
$table = 'user_infor';
$sql = "SELECT id,first_name,last_name,email FROM {$table} WHERE email IS NOT NULL";
$conn1->SetFetchMode(ADODB_FETCH_BOTH);

$results = $conn1->Execute($sql);
$results = RSFilter($results, 'all_caselower');
if ($results == false) {
	print 'error' . $conn1->ErrorMsg() . '<br>';
}

$table_meta = $conn1->MetaColumns($table);
$table_header = array_keys($table_meta);

echo '<table border="1">';
// generate and display header from table field
echo '<tr>';
foreach ($table_header as $colname) {
	echo '<th>' . $colname . '</th>';
}
echo '</tr>';


foreach ($results as $result) {
	echo '<tr>';
	echo '<td>' . $result['id'] . '</td>';
	echo '<td>' . $result['first_name'] . '</td>';
	echo '<td>' . $result['last_name'] . '</td>';
	echo '<td>' . $result['email'] . '</td>';
	echo '</tr>';
}

function all_caselower(&$array, $rs) {
	foreach ($array as $k => $v) {
		if ($k == 'first_name' || $k == 'last_name') {
			$array[$k] = strtolower($v);
		}
	}
}

?>