The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. October 14, 2019, 06:49:36 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Perks dbConnection Class  (Read 3967 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« on: April 26, 2007, 05:44:29 PM »

Generic database connection class - used specifically by the crawler.

Code:
<?php

class dbConnection
{
var $__connected;
var $__host;
var $__user;
var $__password;
var $__database;
var $__myConnection;
var $__lastQuery;
var $dataSet;
var $row;

function dbConnection($host$user$password$database$doConnect=true)
{
$this->__host $host;
$this->__user $user;
$this->__password $password;
$this->__database $database;
$this->__connected false;
}
function &cloneConnection()
{
if (!$this->__connected) { $this->connect(); }
return new dbConnection($this->__host$this->__user$this->__password$this->__database);
}
function close()
{
if($this->__connected) { mysql_close($this->__myConnection); }
}
function connect()
{
$this->__myConnection mysql_connect($this->__host$this->__user$this->__passwordtrue);
if (!$this->__myConnection) { die('class DBConnection cannot connect'); }
$this->__connected true;
mysql_select_db($this->__database$this->__myConnection);
}
function connected() { return $this->__connected; }
function error()
{
if (!$this->__connected) { return ''; }
return mysql_error($this->__myConnection);
}
function fetchArray()
{
// Note that I could have just said "return $this->row" and gotten
// essentially the same result, but I am potentially passing WAY more
// data back than is necessary - especially if there is a TEXT attached to the row
$this->row mysql_fetch_array($this->dataSet);
if ($this->row) { return true; } else { return false; }
}
function fetchRow()
{
$this->row mysql_fetch_row($this->dataSet);
if ($this->row) { return true; } else { return false; }
}
function fieldCount() { return mysql_num_fields($this->dataSet); }
function fieldName($idx) { return mysql_field_name($this->dataSet$idx); }
function lastInsertID() { return $this->singleAnswer('select LAST_INSERT_ID(' $this__myConnection ')'); }
function lastQuery() { return $this->__lastQuery; }
function query($queryStr$ignore=false)
{
if (!$this->__connected) { $this->connect(); }
$this->__lastQuery $queryStr;
$this->__lastError '';
$this->dataSet mysql_query($queryStr$this->__myConnection) or ($this->__lastError mysql_error($this->__myConnection));
if (($this->__lastError ' ') && (!$ignore))
{
print "MySQL Error on query('{$this->__lastQuery}') - {$this->__lastError}";
}
}
        function 
rowCount()
        {
                return 
mysql_num_rows($this->dataSet);
        }
function rowToXML(&$xml)
{
$max mysql_num_fields($this->dataSet);
for ($i=0$i<$max$i++)
{
$xml->addChild(mysql_field_name($this->dataSet$i), $this->row[$i], false);
}
}
function seek($rowNum)
{
return mysql_data_seek($this->dataSet$rowNum);
}
function selectDB($dbName)
{
if (!$this->__connected) { $this->connect(); }
mysql_select_db($dbName$this->__myConnection);
}
function singleAnswer($queryStr)
{
if (!$this->__connected) { $this->connect(); }
$this->query($queryStr);
$this->fetchRow();
return $this->row[0];
}
}

?>

Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
serialnoob
Journeyman
***
Offline Offline

Posts: 88


View Profile
« Reply #1 on: November 29, 2008, 01:22:06 PM »

"rowToXML"

Hello Perky, I am using this class a lot, but I still can't figure out the rowToXML function (I am that thick). Would you care to give an implementation sample?
Logged

Success consists of going from failure to failure without loss of enthusiasm - Winston Churchill
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #2 on: November 29, 2008, 02:23:35 PM »

heh... good eye SN ... that function is a little one I developed that takes advantage of another class of mine, an XML handler. You passed in a reference to an XML instance and the current row is converted to a node and child nodes on an XML tree.

It won't work all alone. Go here and get the XML class: http://www.perkiset.org/forum/php/perks_xml_handler_class-t861.0.html

then do something like this:

Code:
<?php

require "class.xmlhandler.php";
require 
"class.dbconnection.php";

$db = new dbConnection('127.0.0.1''username''password''mydatabase');
$xml = new xmlHandler();
$xml->clear();

$db->query("select * from contacts");
while (
$db->fetchArray())
{
$xml->moveToRoot();
$xml->addChild('row'''true);
$db->rowToXML($xml);
}

?>


That little routine would make an XML record from all the rows in a contacts table (obviously it's pseudocode, and I've not run it to make sure it's OK, but I'm pretty sure)

Hope that helps!
/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: November 29, 2008, 02:25:00 PM »

Also, may I suggest that the new class, here: http://www.perkiset.org/forum/php/perks_mysql_connection_class_updated-t1169.0.html is superior and you'd probably do well to update, although I don't think I included the XML function in it.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
serialnoob
Journeyman
***
Offline Offline

Posts: 88


View Profile
« Reply #4 on: November 29, 2008, 03:21:54 PM »

wishfull thinking! I was just getting into it (separately and  in dispair). Now everything makes sense, thanks!

I meant to ask for months, this place is 7/24/365, right! then when do the avatars sleep (no expecting a feedback though!)

Thanks again
Logged

Success consists of going from failure to failure without loss of enthusiasm - Winston Churchill
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: November 29, 2008, 03:35:32 PM »

The Cache Never Sleeps. Wink

No worries, go well!
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!