• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Architecture for PHP application

I have to design a PHP application. This application will have a GUI. Into this GUI a string will be entered. This string will be processed and persisted in a MySQL Database. Then the application will go and fetch contents of another MySQL table.

I was wondering, in terms of block diagram, how should it be optimally designed?
0
ashima_mahajan
Asked:
ashima_mahajan
  • 2
  • 2
  • 2
  • +1
4 Solutions
 
Steve BinkCommented:
Say you have a form:

<form action="my-string-handler.php" method="POST">
  <input type="textbox" name="mystring" />
  <input type="submit" />
</form>

The application logic would look like this:
<?
// my-string-handler.php
 
if (!isset($_POST['mystring'])) {
  // no string...nothing to do?
}
 
// you'll need code to connect to the server and select the database
// see mysql_connect() and mysql_selectdb()
 
// process the string here, then insert into database
$string = mysql_real_escape_string($_POST['mystring']);
$query = "INSERT INTO MyTable (FieldName) VALUES ('$string')";
if (!($result=mysql_query($query))) {
  // query failed. report and ...
  die();
}
 
// now select another table
$query = "SELECT * FROM MyOtherTable";
if (!($result=mysql_query($query))) {
  // query failed. report and ...
  die();
}
?>

Open in new window

0
 
ashima_mahajanAuthor Commented:
Thanks routinet!

My concern here is, how good an idea is it to directly connect to the database from 1 central bit of code. Pardon me if it is a stupid question. I am a Java developer and am used to structured layers. Like in Java we would write a Data access object to just acess the database and this dao would be then used by the business logic.
0
 
NerdsOfTechTechnology ScientistCommented:
This is acceptable practice in PHP since the code is typically custom and is usually used uniquely.

Only when the code is to be reused several times would it be best practice to create code blocks, functions, or the typical include.

As you can see, coding in PHP is quite different from the explicit drawn-out OO coding side; as most of the coding is BUILT-INTO PHP. Easy!

=NerdsOfTech
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Steve BinkCommented:
I agree with NerdsOfTech.  OOP is fine in its place, and PHP does have some OO constructs available to it, but it is not generally needed.  In your circumstance, removing the database connection to an include file might be a good idea, but that's as far as I would go for something as simple as you are describing.

You're certainly able to create class structures and utilize them in the rest of the app, but the utility in that approach depends on how large of a project you have and how extensible it should be moving forward.  I find that it is best to plan for that in the beginning.  In other words, if you think you'll need it later, code towards that end now.

If you interested in pre-made database classes, there are many available.  I generally find them more trouble than they are worth since they restrict me to coding to someone else's idea of how my database access should work.  They work better for people who like to code, but do not like to fiddle much with the database structures or queries.
0
 
Ray PaseurCommented:
Regarding this, "My concern here is, how good an idea is it to directly connect to the database from 1 central bit of code. "  I would answer this way...

I start EVERY PHP script with a set of statements like this:
<?php // RAY_config_page_usage.php
 
// LOAD THE CONFIG PAGE FROM THE DOCUMENT ROOT
$config_page = $_SERVER["DOCUMENT_ROOT"] . '/config.php';
@include_once($config_page);
 
// LOAD THE CONFIG PAGE FROM THE CURRENT WORKING DIRECTORY
$config_page = getcwd() . '/config.php';
@include_once($config_page);
 
 
 
// REST OF SCRIPT GOES HERE...

Open in new window

0
 
Ray PaseurCommented:
The config pages looks like this.  It performs all the initialization I need and takes care of all the common functions and data definitions.

In some cases, you can look to autoloaders with OOP PHP, but I find that my biggest config pages are rarely so large that you can notice the time it takes to load them, and it helps to know in advance that all my "basic" stuff is take care of.

HTH, ~Ray
<?php // RAY_config_page_example.php - SAMPLE CONFIG SCRIPT
 
// CODE TO PROTECT CONFIG PAGE FROM RUNNING THE SCRIPT STANDALONE
if (count(get_included_files()) < 2)
{
   header("HTTP/1.1 301 Moved Permanently");
   header("Location: /");
   exit;
}
 
// SEE ALL THE ERRORS - NO FALSE ASSUMPTIONS
error_reporting(E_ALL);
 
// START THE SCRIPT TIMER
list ($x, $y) = explode(" ", microtime()); $script_start_time = $x + $y;
 
// ALWAYS START THE SESSION
session_start();
 
// DEFINE MY LOCAL CONSTANTS
DEFINE("API_ID_TOKEN", "PuhCIzE7m2MKraU6HwEwsKu7dNXrCK707tjO7SMLcHy1JZ2CdS");
DEFINE("MAX_FILE_SIZE", "8192000");
 
// ESTALISH MY ENTRY POINT
if (empty($_SERVER["HTTPS"]))
{
	$protocol = 'http://';
}
else
{
	$protocol = 'https://';
}
$my_entry_uri		= $protocol . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
 
// HOOK UP THE DATA BASE
$db_host	= "localhost";
$db_name	= "acct_dbn";
$db_user	= "acct_uid";
$db_word	= "password";
 
// CONNECT TO THE DATA BASE
if (!$db_connection = @mysql_connect("$db_host", "$db_user", "$db_word")) {
	$errmsg	= mysql_errno() . ' ' . mysql_error();
	echo "\n\n\n\n<!-- ! db_connection -->";
	echo "\n<!-- $errmsg -->\n\n\n\n";
	warning_message($errmsg);
}
 
// SELECT THE DATA BASE
if (!$db_sel = @mysql_select_db($db_name, $db_connection)) {
	$errmsg	= mysql_errno() . ' ' . mysql_error();
	echo "\n\n\n\n<!-- ! db_sel -->";
	echo "\n<!-- $errmsg -->\n\n\n\n";
	warning_message($errmsg);
}
 
// LOCAL FUNCTIONS FOLLOW
 
// SEND A WARNING MESSAGE BY EMAIL
function warning_message($subj, $comment="NIL")
{
	$comment .= "\n\n";
	$comment .= "HTTP_USER_AGENT ";
	$comment .= $_ENV["HTTP_USER_AGENT"];
 
	$comment .= "\n\n";
	$comment .= "REMOTE_ADDR ";
	$comment .= $_ENV["REMOTE_ADDR"] . ':' . $_ENV["REMOTE_PORT"];
 
	$comment .= "\n\n";
	$comment .= "REQUEST_URI ";
	$comment .= $_ENV["REQUEST_URI"];
 
	$from	= "From: you@your.org\r";
	mail ( 'you@you.org',	$subj, $comment, $from );
return;
}
 
// FORCE IT TO CHARACTERS ONLY
function clean_alphanum_string($string)
{
	return trim(ereg_replace('[^a-zA-Z0-9_\.\-]', '', $string));
}
 
// ETC. ETC...

Open in new window

0
 
ashima_mahajanAuthor Commented:
Ray_Paseur, routinet, NerdsOfTech.. Thanks a lot! :D
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now