Feedback on page coding.
- digisales
- Newbie


- Joined: Apr 21, 2009
- Posts: 9
- Status: Offline
Over the past several months I have been making an effort to learn PHP on my own. I have created some web pages that call data from mysql, and during my testing of them they all seem to be working just fine.
I'm considering actually using these pages on a website but, since I'm new to this, I don't yet have confidence in them. If possible, I'd like someone to look through the coding I have posted below, and give me their feedback. Basically, what I have done is taken an actual page, stripped out all the HTML, and left only the php.
I am interested in any comments about how this page is structured, but I am especially concerned that I don't have something in it that could disrupt my hosting servers. Throughout the page, various bits of data are being called from two different databases, and I don't want to create a problem of any kind - like leaving connections open for example. Thanks for any help.
I'm considering actually using these pages on a website but, since I'm new to this, I don't yet have confidence in them. If possible, I'd like someone to look through the coding I have posted below, and give me their feedback. Basically, what I have done is taken an actual page, stripped out all the HTML, and left only the php.
I am interested in any comments about how this page is structured, but I am especially concerned that I don't have something in it that could disrupt my hosting servers. Throughout the page, various bits of data are being called from two different databases, and I don't want to create a problem of any kind - like leaving connections open for example. Thanks for any help.
Code: [ Select ]
<HTML><HEAD><TITLE></TITLE>
</HEAD>
<BODY>
<?php
$db = mysql_connect("localhost", "user","pwd");
mysql_select_db("dbase1", $db);
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["f_name"]; echo" ".$myrow["l_name"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["f_name"]; echo" ".$myrow["l_name"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["ref_by"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["co"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["onrf"]; echo" ".$myrow["onrl"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["co"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["onrf"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["onrp"];;
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["onre"];;
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["onrf"];;
?>
--- VARIOUS HTML --------
<?php
$db = mysql_connect("localhost", "user","pwd");
mysql_select_db("DBASE2", $db);
$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT col FROM users WHERE id='X'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["col"];
?>
--- VARIOUS HTML --------
<?php
$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT col FROM users WHERE id='X'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["col"];
?>
--- VARIOUS HTML --------
<?php
$db = mysql_connect("localhost", "user","pwd");
mysql_select_db("dbase1", $db);
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["f_name"]; echo" ".$myrow["l_name"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["f_name"]; echo" ".$myrow["l_name"];
?>
--- VARIOUS HTML --------
</BODY></HTML>
</HEAD>
<BODY>
<?php
$db = mysql_connect("localhost", "user","pwd");
mysql_select_db("dbase1", $db);
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["f_name"]; echo" ".$myrow["l_name"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["f_name"]; echo" ".$myrow["l_name"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["ref_by"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["co"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["onrf"]; echo" ".$myrow["onrl"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["co"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["onrf"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["onrp"];;
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["onre"];;
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["onrf"];;
?>
--- VARIOUS HTML --------
<?php
$db = mysql_connect("localhost", "user","pwd");
mysql_select_db("DBASE2", $db);
$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT col FROM users WHERE id='X'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["col"];
?>
--- VARIOUS HTML --------
<?php
$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT col FROM users WHERE id='X'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["col"];
?>
--- VARIOUS HTML --------
<?php
$db = mysql_connect("localhost", "user","pwd");
mysql_select_db("dbase1", $db);
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["f_name"]; echo" ".$myrow["l_name"];
?>
--- VARIOUS HTML --------
<?php
$a = mysql_real_escape_string($_GET['a']);
$result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["f_name"]; echo" ".$myrow["l_name"];
?>
--- VARIOUS HTML --------
</BODY></HTML>
- <HTML><HEAD><TITLE></TITLE>
- </HEAD>
- <BODY>
- <?php
- $db = mysql_connect("localhost", "user","pwd");
- mysql_select_db("dbase1", $db);
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["f_name"]; echo" ".$myrow["l_name"];
- ?>
- --- VARIOUS HTML --------
- <?php
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["f_name"]; echo" ".$myrow["l_name"];
- ?>
- --- VARIOUS HTML --------
- <?php
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["ref_by"];
- ?>
- --- VARIOUS HTML --------
- <?php
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["co"];
- ?>
- --- VARIOUS HTML --------
- <?php
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["onrf"]; echo" ".$myrow["onrl"];
- ?>
- --- VARIOUS HTML --------
- <?php
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["co"];
- ?>
- --- VARIOUS HTML --------
- <?php
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["onrf"];
- ?>
- --- VARIOUS HTML --------
- <?php
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["onrp"];;
- ?>
- --- VARIOUS HTML --------
- <?php
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["onre"];;
- ?>
- --- VARIOUS HTML --------
- <?php
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["onrf"];;
- ?>
- --- VARIOUS HTML --------
- <?php
- $db = mysql_connect("localhost", "user","pwd");
- mysql_select_db("DBASE2", $db);
- $id = mysql_real_escape_string($_GET['id']);
- $result = mysql_query("SELECT col FROM users WHERE id='X'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["col"];
- ?>
- --- VARIOUS HTML --------
- <?php
- $id = mysql_real_escape_string($_GET['id']);
- $result = mysql_query("SELECT col FROM users WHERE id='X'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["col"];
- ?>
- --- VARIOUS HTML --------
- <?php
- $db = mysql_connect("localhost", "user","pwd");
- mysql_select_db("dbase1", $db);
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["f_name"]; echo" ".$myrow["l_name"];
- ?>
- --- VARIOUS HTML --------
- <?php
- $a = mysql_real_escape_string($_GET['a']);
- $result = mysql_query("SELECT * FROM Xref WHERE a='$a'", $db) or die(mysql_error());
- $myrow = mysql_fetch_array($result);
- echo $myrow["f_name"]; echo" ".$myrow["l_name"];
- ?>
- --- VARIOUS HTML --------
- </BODY></HTML>
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
June 27th, 2009, 12:02 pm
- Nightslyr
- Proficient


- Joined: Sep 21, 2005
- Posts: 274
- Status: Offline
1. In general, your PHP structure is a bit off. While you can freely switch between PHP and HTML in your files, it's generally not a good idea. The reason being that as your pages grow in complexity, it becomes harder to maintain when various technologies are strewn throughout. You should instead process all data first, build up your output, then send out the HTML at the end.
2. Given how many times you execute the same code, you should refactor it. Do you need to continuously take data from the db? Why couldn't you load it all at the beginning, then output it when needed (to, again, echo my first point)? Why two separate db's? Why not different tables in the same db?
3. You should verify that $a is a legit value and gracefully handle it if it's not rather than merely echoing out a mysql error (which are ugly) and quitting the script.
2. Given how many times you execute the same code, you should refactor it. Do you need to continuously take data from the db? Why couldn't you load it all at the beginning, then output it when needed (to, again, echo my first point)? Why two separate db's? Why not different tables in the same db?
3. You should verify that $a is a legit value and gracefully handle it if it's not rather than merely echoing out a mysql error (which are ugly) and quitting the script.
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
You don't need to repeat the codes like you did (as Nightslyr mentioned). You could do most of the PHP stuff before you even put <html>. The only PHP you would have in the actual HTML part is like loops to echo and format arrays.
Or you could learn the use of list() with explode(). Those could come in handy as well.
Or you could learn the use of list() with explode(). Those could come in handy as well.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Nightslyr
- Proficient


- Joined: Sep 21, 2005
- Posts: 274
- Status: Offline
Bogey wrote:
The only PHP you would have in the actual HTML part is like loops to echo and format arrays.
If done right, you wouldn't even need that. There's really no reason why you couldn't build these structures in the processing phase, store them in variables, and simply echo them at the right point in the document. For things like building tables based off db data, it just seems easier to attempt it before any HTML is sent...that way, if there's an error, you could just echo an empty string (where the built-up output would be) followed by an error message.
I'd just rather do something like:
Code: [ Select ]
<?php
//assume I've successfully connected to my db
$results = mysql_query("SELECT * FROM my_database");
$output = "";
$error = "";
if($results)
{
$output .= "<table>";
while($row = mysql_fetch_assoc($results))
{
$output .= "<tr><td>{$row['name']}</td><td>{$row['data']}</td></tr>";
}
$output .= "</table><br />";
}
else
{
$error .= "Could not fetch results from the database. Please contact the webmaster if the problem persists.";
}
?>
<!-- elsewhere in the document -->
<div id="content">
<?php echo $output; ?>
<span id="error"><?php echo $error; ?></span>
</div>
//assume I've successfully connected to my db
$results = mysql_query("SELECT * FROM my_database");
$output = "";
$error = "";
if($results)
{
$output .= "<table>";
while($row = mysql_fetch_assoc($results))
{
$output .= "<tr><td>{$row['name']}</td><td>{$row['data']}</td></tr>";
}
$output .= "</table><br />";
}
else
{
$error .= "Could not fetch results from the database. Please contact the webmaster if the problem persists.";
}
?>
<!-- elsewhere in the document -->
<div id="content">
<?php echo $output; ?>
<span id="error"><?php echo $error; ?></span>
</div>
- <?php
- //assume I've successfully connected to my db
- $results = mysql_query("SELECT * FROM my_database");
- $output = "";
- $error = "";
- if($results)
- {
- $output .= "<table>";
- while($row = mysql_fetch_assoc($results))
- {
- $output .= "<tr><td>{$row['name']}</td><td>{$row['data']}</td></tr>";
- }
- $output .= "</table><br />";
- }
- else
- {
- $error .= "Could not fetch results from the database. Please contact the webmaster if the problem persists.";
- }
- ?>
- <!-- elsewhere in the document -->
- <div id="content">
- <?php echo $output; ?>
- <span id="error"><?php echo $error; ?></span>
- </div>
Instead of:
Code: [ Select ]
<?php
$results = mysql_query("SELECT * FROM my_database");
?>
<!-- ... -->
<div id="content">
<?php
if($results)
{
echo "<table>";
while($row = mysql_fetch_assoc($results))
{
echo "<tr><td>{$row['name']}</td><td>{$row['data']}</td></tr>";
}
echo "</table><br />";
}
else
{
echo "<span id=\"error\">Could not fetch results from the database. Please contact the webmaster if the problem persists.</span>";
}
?>
</div>
$results = mysql_query("SELECT * FROM my_database");
?>
<!-- ... -->
<div id="content">
<?php
if($results)
{
echo "<table>";
while($row = mysql_fetch_assoc($results))
{
echo "<tr><td>{$row['name']}</td><td>{$row['data']}</td></tr>";
}
echo "</table><br />";
}
else
{
echo "<span id=\"error\">Could not fetch results from the database. Please contact the webmaster if the problem persists.</span>";
}
?>
</div>
- <?php
- $results = mysql_query("SELECT * FROM my_database");
- ?>
- <!-- ... -->
- <div id="content">
- <?php
- if($results)
- {
- echo "<table>";
- while($row = mysql_fetch_assoc($results))
- {
- echo "<tr><td>{$row['name']}</td><td>{$row['data']}</td></tr>";
- }
- echo "</table><br />";
- }
- else
- {
- echo "<span id=\"error\">Could not fetch results from the database. Please contact the webmaster if the problem persists.</span>";
- }
- ?>
- </div>
Overall, IMO, it just looks cleaner, and is easier to maintain, if the processing is all done before the output is sent.
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
- Rabid Dog
- Web Master


- Joined: May 21, 2004
- Posts: 3229
- Loc: South Africa
- Status: Offline
Ok here I go again 
Having HTML embedded in code is just a really bad idea. Rather look at implementing a templating mechanism and have your code fill in the blanks if you will.
For things like grid views what you might want to do is define static Strings inside some sort of 'tag' library. that way you can call the static string with a friendly variable name and format it using str_replace or the likes.
Let me know if I should go more indepth with this, hey, prehaps a tutorial is in order!
Having HTML embedded in code is just a really bad idea. Rather look at implementing a templating mechanism and have your code fill in the blanks if you will.
For things like grid views what you might want to do is define static Strings inside some sort of 'tag' library. that way you can call the static string with a friendly variable name and format it using str_replace or the likes.
Let me know if I should go more indepth with this, hey, prehaps a tutorial is in order!
Watch me grow
- SpooF
- ٩๏̯͡๏۶


- Joined: May 22, 2004
- Posts: 3415
- Loc: Richland, WA
- Status: Offline
- Nightslyr
- Proficient


- Joined: Sep 21, 2005
- Posts: 274
- Status: Offline
Rabid Dog wrote:
Ok here I go again 
Having HTML embedded in code is just a really bad idea. Rather look at implementing a templating mechanism and have your code fill in the blanks if you will.
For things like grid views what you might want to do is define static Strings inside some sort of 'tag' library. that way you can call the static string with a friendly variable name and format it using str_replace or the likes.
Let me know if I should go more indepth with this, hey, prehaps a tutorial is in order!
Having HTML embedded in code is just a really bad idea. Rather look at implementing a templating mechanism and have your code fill in the blanks if you will.
For things like grid views what you might want to do is define static Strings inside some sort of 'tag' library. that way you can call the static string with a friendly variable name and format it using str_replace or the likes.
Let me know if I should go more indepth with this, hey, prehaps a tutorial is in order!
FWIW, I think a templating tutorial is a great idea.
- UPSGuy
- Lurker ಠ_ಠ


- Joined: Jul 25, 2005
- Posts: 2735
- Loc: Nashville, TN
- Status: Offline
- Rabid Dog
- Web Master


- Joined: May 21, 2004
- Posts: 3229
- Loc: South Africa
- Status: Offline
- Rabid Dog
- Web Master


- Joined: May 21, 2004
- Posts: 3229
- Loc: South Africa
- Status: Offline
SpooF wrote:
Rabid Dog, do you do much of any PHP work anymore? I'm kind of curious to hear what you have to say about some of the current popular frameworks such as the Zend, Cake and CodeIgniter frameworks in reference to the MVC model and templating.
Unfortunately Spoof I am the guy that recons I can do it better than everyone else
I just find PHP to not be an enterprise solution as it is difficult to find 2am code errors that a compiler picks up for you. That being said I would be interested to have a fiddle with them and render my feed back
Watch me grow
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 11 posts
- Users browsing this forum: No registered users and 125 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
