Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1
This is the forum to discuss the Wrox book Professional PHP5 by Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove; ISBN: 9780764572821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 24th, 2008, 09:50 PM
Registered User
 
Join Date: Jun 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chpt3 - Using MySQL

Hi,

I went through all the code, and changed the necessary database sections to work with MYSQL, though when I try to get the db connection within the mysql_query function i get an error. e.g

$sql = "SELECT * FROM entities";
$res = mysql_query($sql, DataManager::_getConnection());

the above gives me the error

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource

where as if I add the line

$hDB = DataManager::_getConnection();

and change the
$res = mysql_query($sql, DataManager::_getConnection());

to

$res - mysql_query($sql)

it works fine.

Richard


  #2 (permalink)  
Old November 12th, 2009, 10:27 PM
Registered User
 
Join Date: Oct 2009
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

this is my class.DataManager.php

<?php
require_once('class.Entity.php'); //this will be needed later
require_once('class.Individual.php');
require_once('class.Organization.php');

class DataManager
{
private static function _getConnection()
{
static $hDB;

if(isset($hDB))
{
return $hDB;
}

/*$hDB = mysql_connect("localhost", "phpuser", "phppass", "pro_php5")
or
die("Failure connecting to the database!");

return $hDB;*/

$host = 'localhost';

$user = 'phpuser';

$pass = 'phppass';

$database = 'pro_php5';



$dbcnx = @mysql_connect($host, $user, $pass);//connect to the database



if (!$dbcnx)
{
echo 'no connection was made';

exit();//if no connection, exit

}



mysql_select_db($database, $dbcnx);//select database



if (! @mysql_select_db($database) )
{
echo 'no database exits';

exit();//if no database, exit

}

return $dbcnx;
}

public static function getAddressData($addressID)
{
$sql = "SELECT * FROM entityaddress WHERE addressid = $addressID";
$res = mysql_query($sql, DataManager::_getConnection());

if(! ($res && mysql_num_rows($res)))
{
die("Failed getting email data for email $emailID");
}

return mysql_fetch_assoc($res);
}

public static function getEmailData($emailID)
{
$sql = "SELECT * FROM entityemail WHERE emailid = $emailID";
$res = mysql_query($sql, DataManager::_getConnection());

if(! ($res && mysql_num_rows($res)))
{
die("Failed getting email data for email $emailID");
}
return mysql_fetch_assoc($res);
}

public static function getPhoneNumberData($phoneID)
{
$sql = "SELECT * FROM entityphone WHERE phoneid = $phoneID";
$res = mysql_query($sql, DataManager::_getConnection());

if(! ($res && mysql_num_rows($res)))
{
die("Failed getting phone number data for phone $phoneID");
}
return mysql_fetch_assoc($res);
}

public static function getEntityData($entityID)
{
$sql = "SELECT * FROM entities WHERE entityid = $entityID";
$res = mysql_query($sql, DataManager::_getConnection());

if(! ($res && mysql_num_rows($res)))
{
die("Failed getting entity $entityID");
}
return mysql_fetch_assoc($res);
}

public static function getAddressObjectsForEntity($entityID)
{
$sql = "SELECT addressid FROM entityaddress WHERE entityid = $entityID";
$res = mysql_query($sql, DataManager::_getConnection());

if(!res)
{
die("Failed getting address data for entity $entityID");
}

if(mysql_num_rows($res))
{
$objs = array();
while($rec = mysql_fetch_assoc($res))
{
$objs[] = new Address($rec['addressid']);
}
return $objs;
}
else
{
return array();
}
}

public static function getEmailObjectsForEntity($entityID)
{
$sql = "SELECT emailid FROM entityemail WHERE entityid = $entityID";
$res = mysql_query($sql, DataManager::_getConnection());

if(!res)
{
die("Failed getting email data for entity $entityID");
}

if(mysql_num_rows($res))
{
$objs = array();
while($rec = mysql_fetch_assoc($res))
{
$objs[] = new EmailAddress($rec['emailid']);
}
return $objs;
}
else
{
return array();
}
}

public static function getPhoneNumberObjectsForEntity($entityID)
{
$sql = "SELECT phoneid FROM entityphone WHERE entityid = $entityID";
$res = mysql_query($sql, DataManager::_getConnection());

if(!res)
{
die("Failed getting phone data for entity $entityID");
}

if(mysql_num_rows($res))
{
$objs = array();
while($rec = mysql_fetch_assoc($res))
{
$objs[] = new PhoneNumber($rec['phoneid']);
}
return $objs;
}
else
{
return array();
}
}

public static function getEmployer($individualID)
{
$sql = "SELECT organizationid FROM entityemployee WHERE individualid = $individualID";
$res = mysql_query($sql, DataManager::_getConnection());

if(! ($res && mysql_num_rows($res)))
{
die("Failed getting employer info for individual $individualID");
}

$row = mysql_fetch_assoc($res);

if($row)
{
return new Organization($row['organizationid']);
}
else
{
return null;
}
}

public static function getEmployees($orgID)
{
$sql = "SELECT individualid FROM entityemployee WHERE organizationid = $orgID";
$res = mysql_query($sql, DataManager::_getConnection());

if(! ($res && mysql_num_rows($res)))
{
die("Failed getting employee info for org $orgID");
}

if($res)
{
$objs = array();
while( $row = mysql_fetch_assoc($res))
{
$objs[] = new Individual($row['individualid']);
}
return $objs;
}
else
{
return array();
}
}

public static function getAllEntitiesAsObjects()
{
$sql = "SELECT * FROM entities";
$res = mysql_query($sql, DataManager::_getConnection());

if(!$res)
{
die("Failed getting all entities");
}

if(mysql_num_rows($res))
{
$objs = array();
while($row = mysql_fetch_assoc($res))
{
if($row['type'] == 'I')
{
try
{
$objs[] = new Individual($row['entityid']);
}
catch(Exception $e)
{
echo 'Message: ' . $e->getMessage();
}
}
elseif($row['type'] == 'O')
{
try
{
$objs[] = new Organization($row['entityid']);
}
catch(Exception $e)
{
echo 'Message: ' . $e->getMessage();
}
}
else
{
die("Unknown entity type {$row['type']} encountered!");
}
}
return $objs;
}
else
{
return array();
}
}
}
?>

here its the sql

CREATE TABLE entities (
entityid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name1 varchar(100) NOT NULL,
name2 varchar(100) NOT NULL,
type char(1) NOT NULL
);

CREATE TABLE entityaddress (
addressid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
entityid int,
saddress1 varchar(255),
saddress2 varchar(255),
scity varchar(255),
cstate char(2),
spostalcode varchar(10),
stype varchar(50),
CONSTRAINT fk_entityaddress_entityid
FOREIGN KEY (entityid) REFERENCES entity(entityid)
);

CREATE TABLE entityemail (
emailid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
entityid int,
semail varchar(255),
stype varchar(50),
CONSTRAINT fk_entityemail_entityid
FOREIGN KEY (entityid) REFERENCES entity(entityid)
);

CREATE TABLE entityemployee (
individualid INT NOT NULL,
organizationid INT NOT NULL,
CONSTRAINT fk_entityemployee_individualid
FOREIGN KEY (individualid) REFERENCES entity(entityid),
CONSTRAINT fk_entityemployee_organizationid
FOREIGN KEY (organizationid) REFERENCES entity(entityid)
);

CREATE TABLE entityphone (
phoneid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
entityid int,
snumber varchar(20),
sextension varchar(20),
stype varchar(50),
CONSTRAINT fk_entityemail_entityid
FOREIGN KEY (entityid) REFERENCES entity(entityid)
);

Last edited by loremipsum3891; November 12th, 2009 at 10:31 PM.
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why not MySQL ??? mysticav BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 1 August 18th, 2006 04:32 PM
SiteDB - Chpt3 pg87 surfnfrog BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 June 27th, 2006 12:31 AM
mysql rajuru PHP Databases 4 December 7th, 2004 01:40 PM
MySQL 4.0 or 4.1 allang MySQL 1 January 10th, 2004 09:12 AM
mySQL CYcolone Classic ASP Databases 1 August 27th, 2003 02:45 AM



All times are GMT -4. The time now is 08:16 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.