proper way to connect
| From: | Daevid Vincent | Date: | Thu, 24 Aug 2000 21:17:43 +0000 |
| Subject: | proper way to connect | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-13525@lists.php.net to get a copy of this message | ||
What is the proper way to connect to the database in a PHP form that will
keep calling itself and consequently using the database to render parts of
the page? Basically, I have been doing this type of deal where the top line
is:
$linkid = mysql_pconnect( "localhost", "user", "password") or die(
"Unable
to connect to mySQL server");
and then I switch based on the <INPUT BUTTON> the user clicked, which calls
the page over again.
I assumed that the $linkid would be set and that the mysql_pconnect() would
just reset to the same connection it did before, however if I echo $linkid;
it seems to always be "1"? Shouldn't it be something different, sometimes? I
then used the excellent "mytop" program and noticed that every time I
refresh the page, there seems to be another mysqld process spawned...
How do you guys do it?
should I just prefix that mysql_pconnect() above with "if (!isset($linkid))
"
------------------------ snip basic pseudo code -----------------------
<HTML>
<HEAD>
<TITLE>test</TITLE>
<?php include("/www/html/includes/page_top.phtml"); ?>
<?php
Error_Reporting(7); // Set this to 7 for more error reporting...
$linkid = mysql_pconnect( "localhost", "user", "password") or
ie( "Unable to connect to mySQL server");
// execute this chunk-o-code when they hit the button
if ( isset($action) ) {
switch ($action) {
case "Add" :
if ($errstring == "") {
$duplicateResult = mysql_db_query("tattoo", "SELECT ... FROM ...
WHERE ...");
if (!$duplicateResult) { echo "<FONT
COLOR=#00FF00><PRE>".mysql_errno().":
".mysql_error()."</PRE></FONT>\n";
exit; }
$duplicateRows = mysql_num_rows($duplicateResult);
if ( $duplicateRows > 0 )
{
$duplicatesFound = true; // We found some duplicates so dump them
out but leave the existing fields populated
$button_label = "Proceed";
break 1; // break out two levels to bypass the "Proceed" that is
below here...
}
}
case "Proceed" :
if ( $errstring == "" )
{
$sql = "INSERT INTO .... VALUES (...)";
$result = mysql_db_query("tattoo", $sql);
if (!$result) { echo "<FONT COLOR=#FF0000>Add
".$subcategory_name."<P><PRE>".mysql_errno().":
".mysql_error()."<BR></PRE></FONT>\n"; exit; }
}
break;
case "Update" :
if ($errstring == "") {
$updatesql = "UPDATE ... SET ...";
$result = mysql_db_query("tattoo", $updatesql);
if (!$result) { echo "<FONT COLOR=#0000FF>Update
".$subcategory_fullname."<P><PRE>".mysql_errno().":
".mysql_error()."<BR>".$updatesql."</PRE></FONT>\n";
exit; }
}
break;
}
} // if Request == POST
if ($errstring != "") echo "<FONT SIZE=+1
COLOR=#FF0000>".$errstring."</FONT>";
?>
<FORM Method="Post">
<INPUT Name="subcategory_name" Size="30" Value='<?php echo
$subcategory_name;?>'>
<INPUT Name="subcategory_directory" Size="30" Value='<?php echo
strtolower($subcategory_directory);?>'>
<INPUT NAME="action" TYPE="SUBMIT" VALUE='<?php echo $button_label;
?>'>
<INPUT NAME="action" TYPE="SUBMIT" VALUE="Edit">
<?php ListboxMatch(1, "edit_subcategory_id", "SELECT subcategory_id,
subcategory_name FROM SubCategory_Table ORDER BY subcategory_name",
$edit_subcategory_id); ?>
</FORM>
<?php include("/www/html/includes/page_bottom.phtml"); ?>