Solved

Architecture for PHP application

Posted on 2009-05-06
7
241 Views
Last Modified: 2013-12-12
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
Comment
Question by:ashima_mahajan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 200 total points
ID: 24322335
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
 

Author Comment

by:ashima_mahajan
ID: 24323018
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
 
LVL 19

Assisted Solution

by:NerdsOfTech
NerdsOfTech earned 100 total points
ID: 24323237
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 50

Expert Comment

by:Steve Bink
ID: 24323444
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
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 24328206
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24328236
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
 

Accepted Solution

by:
ashima_mahajan earned 0 total points
ID: 24334590
Ray_Paseur, routinet, NerdsOfTech.. Thanks a lot! :D
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question