Forum Moderators: coopster
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]
<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>
[code]
If this is just a single page then you don't need to query every time, query once, save the values to a set of variables, then you can use them as often as you like throughout the page. You also don't need to reconnect when you want to switch dbs. You had some double semi colons and some double echoes that were unnecessary. You want to set everything up at the top and then just echo your vars n the page
the following code should work exactly the same as what you have
<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);
$fullname = $myrow["f_name"] . " " . $myrow["l_name"];
$ref_by = $myrow["ref_by"];
$co = $myrow["co"];
$onrf = $myrow["onrf"];
$onrl = $myrow["onrl"];
$onre = $myrow["onre"];
$onrp = $myrow["onrp"];
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);
$col = $myrow["col"];
echo $fullname;
?>
--- VARIOUS HTML --------
<?php echo $fullname; ?>
--- VARIOUS HTML --------
<?php echo $ref_by; ?>
--- VARIOUS HTML --------
<?php echo $co; ?>
--- VARIOUS HTML --------
<?php echo $onrf, " ", $onrl; ?>
--- VARIOUS HTML --------
<?php echo $co; ?>
--- VARIOUS HTML --------
<?php echo $onrf; ?>
--- VARIOUS HTML --------
<?php echo $onrp; ?>
--- VARIOUS HTML --------
<?php echo $onre; ?>
--- VARIOUS HTML --------
<?php echo $onrf; ?>
--- VARIOUS HTML --------
<?php echo $col; ?>
--- VARIOUS HTML --------
<?php echo $col; ?>
--- VARIOUS HTML --------
<?php echo $fullname; ?>
--- VARIOUS HTML --------
<?php echo $fullname; ?>
--- VARIOUS HTML --------
</BODY></HTML>
I would also suggest using variable names that are a little more descriptive or at very least not so close to the same, it will become very easy to confuse these
$onrf
$onrl
$onre
$onrp
The script can then send the 404 if there is no database content to send, or else move on to sending the DOCTYPE and opening <html> tag, followed by all the code and content for the visible page.
That is, once you have sent the <html> tag out (and currently that is the very first thing that happens) then it is far too late to be sending any sort of HTTP HEADER information out to the browser.
Another reason for doing this, is that you'll want to populate the page title and meta description from the database too.
I don't want to create a problem of any kind - like leaving connections open for example