Forum Moderators: coopster

Message Too Old, No Replies

Leaving open connections, etc.

         

digisales

8:02 pm on Jun 27, 2009 (gmt 0)

10+ Year Member



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 them on a website but, since I'm new to this, I don't yet have confidence in them. If possible, I would like someone to look through the coding I have posted, 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]
<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]

jatar_k

2:17 pm on Jun 29, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it seems you're repeating things alot, I assume it isn't a pasting error but that you are selecting and connecting every time.

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

digisales

5:22 pm on Jun 29, 2009 (gmt 0)

10+ Year Member



Wow, you have been an incredible help! I knew there had to be a way to simplifify this, but was not sure how to go about it. You obviously invested some time in this, and I am very grateful -thank you.

jatar_k

6:12 pm on Jun 29, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, as much as I would love to take credit for a vast contribution it only took about 10 minutes.

It's been a few years and I know what to look for ;)

but it was still my pleasure, we are here to help after all

g1smd

6:58 pm on Jun 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Since you will want to return a "404 Not Found" response by using a HEADER command if there is nothing in the database to fulfill any particular URL request, you may want to move all the stuff that reads from the database so that it happens *before* you send the DOCTYPE and opening <html> tag out to the browser.

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.

AlexK

7:23 pm on Jun 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



digisales:
I don't want to create a problem of any kind - like leaving connections open for example

One of the beauties of using a scripting program like PHP is that it will auto-fix such issues for you: all open MySQL connections will be closed, all memory allocated for variables will be returned to heap, etc, etc.