PHP ADODB FETCH BOTH
From w3cyberlearnings
Contents |
PHP ADODB_FETCH_BOTH
Fetch records as index and associative array. This can be very useful when you need to retrieve or access as associative array and index.
Syntax ADODB
$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); // access as associative array foreach($results as $result) { echo $result['id'] . '<br/>'; echo $result['first_name'] . '<br/>'; echo $result['last_name'] . '<br/>'; echo $result['email'] . '<br/>'; } // access as index foreach($results as $result) { echo $result[0] . '<br/>'; echo $result[1] . '<br/>'; echo $result[2] . '<br/>'; echo $result[3] . '<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_BOTH); $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[0] . '</td>'; echo '<td>' . $result[1] . '</td>'; echo '<td>' . $result[2] . '</td>'; echo '<td>' . $result[3] . '</td>'; 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