Jump to: navigation, search

PHP ADODB FETCH ASSOC

From w3cyberlearnings

Contents

PHP ADODB FETCH ASSOC

Fetch records as associative array.

Syntax ADODB_FETCH_ASSOC

$table = 'user_infor';
$sql = "SELECT id,first_name,last_name,email FROM {$table} WHERE email IS NOT NULL";
$conn1->SetFetchMode(ADODB_FETCH_ASSOC);
$results = $conn1->Execute($sql);
foreach($results as $result) {
	echo  $result['id'] . '<br/>';
	echo  $result['first_name'] . '<br/>';
	echo  $result['last_name'] . '<br/>';
	echo  $result['email'] . '<br/>';
	
}

Example 1

<?php

include 'adodb5/adodb.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_ASSOC);

$results = $conn1->Execute($sql);

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>';
}
?>

Output

Php mysql adodb select 1.png


Related Links


Navigation
Web
SQL
MISC
References