PHP Classes
elePHPant
Icontem

File: example_select.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Evert Ulises German Soto  >  PDO Multi Connection Class  >  example_select.php  >  Download  
File: example_select.php
Role: Example script
Content type: text/plain
Description: example_select.php
Class: PDO Multi Connection Class
Access different types of SQL database using PDO
Author: By
Last change: How to Get the recordset in Object.
Date: 2 years ago
Size: 968 bytes
 

Contents

Class file image Download
<?php
require("pdo_database.class.php");

$db = new wArLeY_DBMS("mysql", "127.0.0.1", "test", "root", "", "");
$dbCN = $db->Cnxn(); //This step is really neccesary for create connection to database, and getting the errors in methods.
if($dbCN==false) die("Error: Cant connect to database.");
echo
$db->getError(); //Show error description if exist, else is empty.

$rs = $db->query("SELECT NAME,ADDRESS FROM TB_USERS");
foreach(
$rs as $row){
   
$tmp_name = $row["NAME"];
   
$tmp_address = $row["ADDRESS"];
    echo
"The user $tmp_name lives in: $tmp_address<br/>";
}

//But if you need retrieve rows in objects, not in array... you need specify like this...
$rs = $db->query("SELECT NAME,ADDRESS FROM TB_USERS");
$rs->setFetchMode(PDO::FETCH_OBJ); // <---- This is most important!
foreach($rs as $row){
   
$tmp_name = $row->NAME;
   
$tmp_address = $row->ADDRESS;
    echo
"The user $tmp_name lives in: $tmp_address<br/>";
}
$rs = null;
$db->disconnect();
?>