logo       

[SMARTY] Thanks for your help everyone: msg#00418

php.smarty.general

Subject: [SMARTY] Thanks for your help everyone

This list has been very helpful to me. I am providing this code without
warranty of any kind... but I have used it on several projects now, so I can
tell you that it has worked for me and saved me a ton of time. I hope you
get some benefit from it - I'm sure most of you have a much greater
knowledge of PHP than I do, but maybe this will save you some time one day.

Over the past year, I've written a collection of tools that make it much
simpler to handle mysql queries, simplify the code in pages, and generally
increase efficiency. Complete source code to four files pasted below. The
code takes the standard mysql_ PHP functions a bit further than the examples
shown on php.net, but not as complex as PEAR. Using the supplied objects,
you can issue a database query in one line of code -- for example
include("qry_somequery.php"); In the example below, you would then use
$resultArray in your page after the include. If an error occurred, it would
be available by calling $mysqlObj->getError();

At the start of the project, edit your db credentials in
inc_credentials.php. Copy the sample qry_somequery.php file and modify the
SQL command it issues, and include it or require it on your page.
Alternatively, you can do logic in your page to build a $dynamicSQL query
and then remove the sql code from the file and use the variable
($dynamicSQL) instead. This works well inside a repeat loop in certain
circumstances.

I learned Fusebox before I learned Smarty... the naming conventions and the
way Fusebox recommends separating display code from query code works well in
a Smarty application too.

Enjoy - and feel free to provide feedback and/or bug fixes!
Geoff

===================================================================
// Code below. this is some info of what's there
inc_credentials.php {
A short php file that stores your username, password,
database name, and database path outside the web root
}
qry_somequery.php {
A sample usage of the included objects, intended to be
included on your page
}
mysqlCommon.class.php {
A generic mysql class that returns an associative array
instead of mysql_result resources
}
mysqlUser.class.php {
Object to store username and passord, and is passed by reference
to the mysqlCommon class constructor
}
===================================================================
<?php
// inc_credentials.php
// this is the single file that has the username and pwd in it...
// store this OUTSIDE YOUR WEB ROOT!
require_once("mysqlUser.class.php");
$db = "your_database_name";
$path = "your_database_path";
$usr = new mysqlUser("your_username", "your_password");
?>
===================================================================
<?php
// qry_somequery.php
// this can be included in the directory of the calling file (.)
require_once("../path/to/your/inc_credentials.php");
require_once("../path/to/your/mysqlCommon.class.php");
$aQRY = "

SELECT
*

FROM
sometable

WHERE
0=0

";
$mysqlObj = new mysqlCommon($usr, $path, $db, $aQRY, NULL, NULL);
$resultArray = $mysqlObj->action();
?>
===================================================================
<?php
/*
mysqlUser.class.php
Store this file OUTSIDE THE WEB ROOT!
A wrapper class for storing db login credentials
*/

class mysqlUser {

var $username;
var $password;

function mysqlUser( $_userName, $_password ) {
$this->setusername( $_userName );
$this->setpassword( $_password );
}

function getusername( ) {
return $this->username;
}

function setusername( $_usr ) {
$this->username = $_usr;
}

function getpassword( ) {
return $this->password;
}

function setpassword( $_pwd ) {
$this->password = $_pwd;
}

}

?>
===================================================================
<?php
/*
MySQL Common Class v1.2
By Geoff Hoffman geoff@xxxxxxxxxxxx

I'm an object designed to be used as a multipurpose SQL command robot
that can work alone or as a collection.

To use me alone,
- call the class constructor passing in values for a single SQL query,
- then set a local variable equal to my action() method.

To use me along with others, make two new arrays $objects and $results.
Open a connection outside a loop and pass in the linkId
Use $objects to hold instances of me. Use $results as an empty array
which is passed into the constructor for each instance.
Loop over $objects, calling all of our action() methods, and when complete,
$results will be populated with resource result IDs.

v1.0 base class written
v1.1 added error handling
v1.2 added lastInsertId for inserts 2003-01-13

*/


class mysqlCommon {

var $path;
var $linkId;
var $dbname;
var $username;
var $passwd;
var $sql;
var $retVal;
var $err;
var $lastInsertId;

// CONSTRUCTOR
function mysqlCommon(&$_usrobj, $_path, $_dbname, $_sql, $_linkId,
$_retVal)
{
$this->username = $_usrobj->getusername();
$this->passwd = $_usrobj->getpassword();
$this->path = $_path;
$this->dbname = $_dbname;
$this->sql = $_sql;
$this->linkId = $_linkId;
$this->retVal = $_retVal;
$this->err = NULL;
$this->lastInsertId = NULL;

}


function mcConnect( )
{
// set the linkId property
$this->linkId = $this->mcGetLinkId();
}

function mcGetLinkId( )
{
// connect to the db to get the linkId
$test = mysql_connect($this->path, $this->username,
$this->passwd) or $this->err = "Could Not Connect.";
return $test;
}

function isConnected( )
{
// tell whether I am connected to the db
if($this->linkId == NULL)
{
return false;
} else {
return true;
}
}

function action( )
{
//alias to mcAction
$this->mcAction();
return $this->retVal;
}

function mcAction(){
// the first step is to get a connection to the db, unless we
already have one.
// this object can be used in part of a multi-query loop by passing
in a valid
// linkId, setting the properties of each, and then calling the
action functions for each.

if (! $this->isConnected())
{
$this->mcConnect();
}

// USE the correct database
mysql_select_db($this->dbname, $this->linkId) or $this->err
= "Could not select DB: ".$this->dbname;

// test whether the returnValue passed in was NULL, if so
just perform the query,
// if an array was passed, perform the query and add the
resultset onto the array.

$gt = gettype($this->retVal);
switch ($gt ) {

case "array" :
$t = mysql_query($this->sql, $this->linkId);
if (gettype($t)=="resource")
{
while($row=mysql_fetch_assoc($t))
{
array_push($this->retVal,
$row);
}
} else {
array_push($this->retVal, $t);
}
$this->err = mysql_error($this->linkId);
break;

case "NULL" :
//echo "action called, retVal supplied =
null"
$t = mysql_query($this->sql, $this->linkId);
$ar = array();
if (gettype($t) == "resource" )
{
while($row = mysql_fetch_assoc($t)){
array_push($ar, $row);
}
$this->retVal = $ar ;
} else {
array_push($ar, $t);
}
$this->err = mysql_error($this->linkId);
if(strstr($this->sql, "INSERT")){
$this->lastInsertId =
mysql_insert_id($this->linkId);
}
break;
}
}

function getError() {
return $this->err;
}
}
?>


--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise