Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- id name
- 1 Herbert
- 2 LG
- 3 Levins
- <?php
- $dsn = 'mysql:host=localhost;dbname=postgre';
- $username = 'root';
- $password = '';
- $options = array(
- PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
- );
- $db = new PDO($dsn, $username, $password, $options);
- $stmt = $db->query("SELECT * FROM friends");
- $cnt_columns = $stmt->columnCount();
- for($i = 0; $i < $cnt_columns; $i++) {
- $metadata = $stmt->getColumnMeta($i);
- var_dump($metadata);
- }
- ?>
- array
- 'native_type' => string 'LONG' (length=4)
- 'pdo_type' => int 2
- 'flags' =>
- array
- empty
- 'table' => string 'friends' (length=7)
- 'name' => string 'id' (length=2)
- 'len' => int 11
- 'precision' => int 0
- array
- 'native_type' => string 'VAR_STRING' (length=10)
- 'pdo_type' => int 2
- 'flags' =>
- array
- empty
- 'table' => string 'friends' (length=7)
- 'name' => string 'name' (length=4)
- 'len' => int 60
- 'precision' => int 0
- id name
- 1 Hebert
- 2 LG
- 3 Levins
- $stmt = $db->query("SELECT * FROM friends");
- $cnt_columns = $stmt->columnCount();
- for($i = 0; $i < $cnt_columns; $i++) {
- $metadata = $stmt->getColumnMeta($i);
- var_dump($metadata);
- }
- $sql = "SELECT * FROM friends";
- $stmt = $db->prepare($sql);
- $stmt->execute();
- $result = $stmt->fetchAll();
- // field names
- if(count($result) > 0) {
- foreach($result[0] as $k => $v) {
- if(!is_int($k)) {
- echo $k . "t";
- }
- }
- }
- echo PHP_EOL;
- // data
- foreach ($result as $row) {
- foreach($row as $k => $v) {
- if(!is_int($k)) {
- echo $row[$k] . "t";
- }
- }
- echo PHP_EOL;
- }
Add Comment
Please, Sign In to add comment