I need Help [message #169686] |
Wed, 22 September 2010 20:23 |
Patrick Ehrmann
Messages: 1 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
Hi,
I have a problem with the script below! Unfortunately I can not find
the error! The connection to the server works and also to the
database. But no values are written into the table!
Can you help me?
Thank you
PE
**************************************************
<?php
/*****************************
*verbindung mit der Datenbank*
*****************************/
$verbindung = mysql_connect("localhost","******","******") or die
("keine Verbindung möglich. Benutzernahme und Kennwort sind Falsch");
mysql_select_db("usr_web216_1") or die ("DB nicht gefunden");
/****************************
*** Einlesen Formular********
*****************************/
$dojoid = $_POST['dojonr'];
$pruefungs_ort = $_POST['port'];
$datum = $_POST['pdatum'];
$name = $_POST['name'];
$vorname = $_POST['vorname'];
$alter = $_POST['alter'];
$seilchenspringen = $_POST['seil'];
$dreiersprung = $_POST['dreisprung'];
$knieliegestütz = $_POST['knieliege'];
$rückenmuskel = $_POST['ruekenm'];
$stand_h_sprung = $_POST['stand_h_sprung'];
$medizinball_ws = $_POST['medizin'];
$japanlauf = $_POST['japanlauf'];
$crunch = $_POST['crunch'];
$laufdis = $_POST['laufdis'];
$laufzeit = $_POST['laudzeit'];
/***************************
***Eintrag in die Tabelle***
****************************/
$eintrag = "INSERT INTO usr_web216_1, bl_auswertung (id, Dojo_id,
Pruefungs_ort, Pruefungs_datum, Name, Vorname, Alter,
Seilchenspringen, Dreiersprung, Knieliegestuetz, Rueckenmuskeltest,
Stand_h_sprung, Medizinball_ws, Japanlauf, Crunch, Laufdis, Laufzeit)
VALUES ('', '$dojoid', '$pruefungs_ort', '$datum', '$name',
'$vorname', '$alter', '$seilchenspringen', '$dreiersprung',
'$knieliegestütz', '$rückenmuskel', '$stand_h_sprung',
'$medizinball_ws', '$japanlauf', '$crunch', '$laufdis', '$laufzeit')";
/*
$eintrag = "INSERT INTO `usr_web216_1`,`bl_auswertung` (`id`,
`Dojo_id`, `Pruefungs_ort`, `Pruefungs_datum`, `Name`, `Vorname`,
`Alter`, `Seilchenspringen`, `Dreiersprung`, `Knieliegestuetz`,
`Rueckenmuskeltest`, `Stand_h_sprung`, `Medizinball_ws`, `Japanlauf`,
`Crunch`, `Laufdis`, `Laufzeit`)
VALUES
(\'\', \'$dojoid\', \'$pruefungs_ort\', \'$datum\', \'$name\',
\'$vorname\', \'$alter\', \'$seilchenspringen\', \'$dreiersprung\',
\'$knieliegestütz\', \'$rückenmuskel\', \'$stand_h_sprung\',
\'$medizinball_ws\', \'$japanlauf\', \'$crunch\', \'$laufdis\',
\'$laufzeit\')";*/
$eintragen = mysql_query($eintrag);
if($eintragen == true)
{
echo "Eintrag war Erfolgreich";
}
else
{
echo "Fehler beim Speichern";
}
mysql_close ($verbindung);
?>
<html>
<head> <title>Leistung</title> <meta http-equiv="refresh" content="1;
URL=../Leistungen.html"> <meta name="keywords" content="automatic
redirection"> </head>
<body> Sie werden umgeleitet.
<?php if (empty($fehler)) {
echo "Eintrag erfolgreich";
} ?>
</body>
</html>
|
|
|
Re: I need Help [message #169687 is a reply to message #169686] |
Wed, 22 September 2010 20:38 |
me
Messages: 192 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/22/2010 4:23 PM, Patrick Ehrmann wrote:
> Hi,
>
> I have a problem with the script below! Unfortunately I can not find
> the error! The connection to the server works and also to the
> database. But no values are written into the table!
> Can you help me?
>
> Thank you
Is this intentional, the entire block turned into a comment?
>
> /*
> $eintrag = "INSERT INTO `usr_web216_1`,`bl_auswertung` (`id`,
> `Dojo_id`, `Pruefungs_ort`, `Pruefungs_datum`, `Name`, `Vorname`,
> `Alter`, `Seilchenspringen`, `Dreiersprung`, `Knieliegestuetz`,
> `Rueckenmuskeltest`, `Stand_h_sprung`, `Medizinball_ws`, `Japanlauf`,
> `Crunch`, `Laufdis`, `Laufzeit`)
> VALUES
> (\'\', \'$dojoid\', \'$pruefungs_ort\', \'$datum\', \'$name\',
> \'$vorname\', \'$alter\', \'$seilchenspringen\', \'$dreiersprung\',
> \'$knieliegestütz\', \'$rückenmuskel\', \'$stand_h_sprung\',
> \'$medizinball_ws\', \'$japanlauf\', \'$crunch\', \'$laufdis\',
> \'$laufzeit\')";*/
Bill B
|
|
|
Re: I need Help [message #169688 is a reply to message #169686] |
Wed, 22 September 2010 20:46 |
Robert Hairgrove
Messages: 19 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
Patrick Ehrmann wrote:
> Hi,
>
> I have a problem with the script below! Unfortunately I can not find
> the error! The connection to the server works and also to the
> database. But no values are written into the table!
> Can you help me?
>
> Thank you
>
> PE
>
You could use mysql_error() after checking the return value of
mysql_query() to find out why...
> **************************************************
> <?php
>
> /*****************************
> *verbindung mit der Datenbank*
> *****************************/
>
> $verbindung = mysql_connect("localhost","******","******") or die
> ("keine Verbindung möglich. Benutzernahme und Kennwort sind Falsch");
> mysql_select_db("usr_web216_1") or die ("DB nicht gefunden");
>
>
> /****************************
> *** Einlesen Formular********
> *****************************/
> $dojoid = $_POST['dojonr'];
> $pruefungs_ort = $_POST['port'];
> $datum = $_POST['pdatum'];
>
> $name = $_POST['name'];
> $vorname = $_POST['vorname'];
> $alter = $_POST['alter'];
>
> $seilchenspringen = $_POST['seil'];
> $dreiersprung = $_POST['dreisprung'];
> $knieliegestütz = $_POST['knieliege'];
^
I'm not sure, but I think it's not allowed to use anything but ASCII
characters in PHP variable names. However, I may be wrong.
> $rückenmuskel = $_POST['ruekenm'];
> $stand_h_sprung = $_POST['stand_h_sprung'];
> $medizinball_ws = $_POST['medizin'];
> $japanlauf = $_POST['japanlauf'];
> $crunch = $_POST['crunch'];
> $laufdis = $_POST['laufdis'];
> $laufzeit = $_POST['laudzeit'];
>
Here it would be very wise to check the user input for malicious code
(Google for "SQL injection"...)
>
> /***************************
> ***Eintrag in die Tabelle***
> ****************************/
>
> $eintrag = "INSERT INTO usr_web216_1, bl_auswertung (id, Dojo_id,
^^^
This should be:
$eintrag = "INSERT INTO usr_web216_1.bl_auswertung (...)
You have a comma and extra space between your schema name and the name
of the table. It should be a period (full stop) instead of a comma, and
no space. And since you already selected the database after connecting,
you don't necessarily need to prefix the table name with the schema name
(although I think it is a good habit to do so).
> Pruefungs_ort, Pruefungs_datum, Name, Vorname, Alter,
> Seilchenspringen, Dreiersprung, Knieliegestuetz, Rueckenmuskeltest,
> Stand_h_sprung, Medizinball_ws, Japanlauf, Crunch, Laufdis, Laufzeit)
> VALUES ('', '$dojoid', '$pruefungs_ort', '$datum', '$name',
> '$vorname', '$alter', '$seilchenspringen', '$dreiersprung',
> '$knieliegestütz', '$rückenmuskel', '$stand_h_sprung',
> '$medizinball_ws', '$japanlauf', '$crunch', '$laufdis', '$laufzeit')";
> /*
> $eintrag = "INSERT INTO `usr_web216_1`,`bl_auswertung` (`id`,
^^^
Same thing...
You really don't need backticks unless you are using reserved words as
identifiers. I don't like them myself; PhpMyAdmin always sticks them on,
but probably because it doesn't really know what names are being used
and can't take chances.
> `Dojo_id`, `Pruefungs_ort`, `Pruefungs_datum`, `Name`, `Vorname`,
> `Alter`, `Seilchenspringen`, `Dreiersprung`, `Knieliegestuetz`,
> `Rueckenmuskeltest`, `Stand_h_sprung`, `Medizinball_ws`, `Japanlauf`,
> `Crunch`, `Laufdis`, `Laufzeit`)
> VALUES
> (\'\', \'$dojoid\', \'$pruefungs_ort\', \'$datum\', \'$name\',
> \'$vorname\', \'$alter\', \'$seilchenspringen\', \'$dreiersprung\',
> \'$knieliegestütz\', \'$rückenmuskel\', \'$stand_h_sprung\',
> \'$medizinball_ws\', \'$japanlauf\', \'$crunch\', \'$laufdis\',
> \'$laufzeit\')";*/
>
>
> $eintragen = mysql_query($eintrag);
> if($eintragen == true)
> {
> echo "Eintrag war Erfolgreich";
> }
> else
> {
Here you should call mysql_error()...
> echo "Fehler beim Speichern";
> }
>
> mysql_close ($verbindung);
PHP takes care of this automatically, I believe.
>
>
> ?>
> <html>
> <head> <title>Leistung</title> <meta http-equiv="refresh" content="1;
> URL=../Leistungen.html"> <meta name="keywords" content="automatic
> redirection"> </head>
> <body> Sie werden umgeleitet.
> <?php if (empty($fehler)) {
> echo "Eintrag erfolgreich";
> } ?>
> </body>
> </html>
>
>
|
|
|
Re: I need Help [message #169692 is a reply to message #169688] |
Thu, 23 September 2010 03:24 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/22/2010 4:46 PM, Robert Hairgrove wrote:
> Patrick Ehrmann wrote:
>> mysql_close ($verbindung);
>
> PHP takes care of this automatically, I believe.
>
Never a good idea to allow PHP to close the connection. Close it
yourself when you're done!
>>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: I need Help [message #169693 is a reply to message #169692] |
Thu, 23 September 2010 04:17 |
Beauregard T. Shagnas
Messages: 154 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
Jerry Stuckle wrote:
> Robert Hairgrove wrote:
>> Patrick Ehrmann wrote:
>>> mysql_close ($verbindung);
>>
>> PHP takes care of this automatically, I believe.
>
> Never a good idea to allow PHP to close the connection. Close it
> yourself when you're done!
Rule 13:
open the db connection
do your query, your select, update, insert, whatever
close the db connection
proceed with other coding
--
-bts
-okay .. it may be rule 9 or rule 2 or ...
|
|
|
Re: I need Help [message #169695 is a reply to message #169692] |
Thu, 23 September 2010 07:47 |
Robert Hairgrove
Messages: 19 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
Jerry Stuckle wrote:
> On 9/22/2010 4:46 PM, Robert Hairgrove wrote:
>> Patrick Ehrmann wrote:
>>> mysql_close ($verbindung);
>>
>> PHP takes care of this automatically, I believe.
>>
>
> Never a good idea to allow PHP to close the connection. Close it
> yourself when you're done!
This is true for persistent connections. However:
http://ch2.php.net/manual/en/function.mysql-close.php
"(...) Using mysql_close() isn't usually necessary, as non-persistent
open links are automatically closed at the end of the script's
execution. See also freeing resources."
|
|
|
Re: I need Help [message #169696 is a reply to message #169695] |
Thu, 23 September 2010 07:56 |
Robert Hairgrove
Messages: 19 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
Robert Hairgrove wrote:
> Jerry Stuckle wrote:
>> On 9/22/2010 4:46 PM, Robert Hairgrove wrote:
>>> Patrick Ehrmann wrote:
>>>> mysql_close ($verbindung);
>>>
>>> PHP takes care of this automatically, I believe.
>>>
>>
>> Never a good idea to allow PHP to close the connection. Close it
>> yourself when you're done!
>
> This is true for persistent connections. However:
>
> http://ch2.php.net/manual/en/function.mysql-close.php
>
> "(...) Using mysql_close() isn't usually necessary, as non-persistent
> open links are automatically closed at the end of the script's
> execution. See also freeing resources."
Looking at some of the user comments, it seems that PHP applications on
Windows connecting via TCP might have a problem in this regard. However,
if you are connecting to the DB on localhost, e.g. on a Linux server
hosting a website, then this shouldn't be a problem.
From reading the rest of the problems posted there by users, looks like
it is difficult to get it right -- best to let PHP take care of it
unless there is a compelling reason otherwise.
|
|
|
Re: I need Help [message #169699 is a reply to message #169696] |
Thu, 23 September 2010 13:20 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/23/2010 3:56 AM, Robert Hairgrove wrote:
> Robert Hairgrove wrote:
>> Jerry Stuckle wrote:
>>> On 9/22/2010 4:46 PM, Robert Hairgrove wrote:
>>>> Patrick Ehrmann wrote:
>>>> > mysql_close ($verbindung);
>>>>
>>>> PHP takes care of this automatically, I believe.
>>>>
>>>
>>> Never a good idea to allow PHP to close the connection. Close it
>>> yourself when you're done!
>>
>> This is true for persistent connections. However:
>>
>> http://ch2.php.net/manual/en/function.mysql-close.php
>>
>> "(...) Using mysql_close() isn't usually necessary, as non-persistent
>> open links are automatically closed at the end of the script's
>> execution. See also freeing resources."
>
> Looking at some of the user comments, it seems that PHP applications on
> Windows connecting via TCP might have a problem in this regard. However,
> if you are connecting to the DB on localhost, e.g. on a Linux server
> hosting a website, then this shouldn't be a problem.
>
> From reading the rest of the problems posted there by users, looks like
> it is difficult to get it right -- best to let PHP take care of it
> unless there is a compelling reason otherwise.
Except for extremely rare instances, you shouldn't use persistent
connections in PHP. They will almost always harm performance more than
they will help.
And whether the script will close connections when it ends or not, it is
still not a good idea to depend on the system to close it for you. This
is true with all languages and all databases.
Good programming practices means to always clean up after yourself and
don't depend on someone else to do it.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: I need Help [message #169707 is a reply to message #169699] |
Thu, 23 September 2010 17:01 |
Robert Hairgrove
Messages: 19 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
Jerry Stuckle wrote:
> On 9/23/2010 3:56 AM, Robert Hairgrove wrote:
>> Robert Hairgrove wrote:
>>> Jerry Stuckle wrote:
>>>> On 9/22/2010 4:46 PM, Robert Hairgrove wrote:
>>>> > Patrick Ehrmann wrote:
>>>> >> mysql_close ($verbindung);
>>>> >
>>>> > PHP takes care of this automatically, I believe.
>>>> >
>>>>
>>>> Never a good idea to allow PHP to close the connection. Close it
>>>> yourself when you're done!
>>>
>>> This is true for persistent connections. However:
>>>
>>> http://ch2.php.net/manual/en/function.mysql-close.php
>>>
>>> "(...) Using mysql_close() isn't usually necessary, as non-persistent
>>> open links are automatically closed at the end of the script's
>>> execution. See also freeing resources."
>>
>> Looking at some of the user comments, it seems that PHP applications on
>> Windows connecting via TCP might have a problem in this regard. However,
>> if you are connecting to the DB on localhost, e.g. on a Linux server
>> hosting a website, then this shouldn't be a problem.
>>
>> From reading the rest of the problems posted there by users, looks like
>> it is difficult to get it right -- best to let PHP take care of it
>> unless there is a compelling reason otherwise.
>
> Except for extremely rare instances, you shouldn't use persistent
> connections in PHP. They will almost always harm performance more than
> they will help.
>
> And whether the script will close connections when it ends or not, it is
> still not a good idea to depend on the system to close it for you. This
> is true with all languages and all databases.
>
> Good programming practices means to always clean up after yourself and
> don't depend on someone else to do it.
Many other popular languages such as Java, C# and C++ also have what is
known as a "garbage collection". With C++, of course, you can opt out of
it (or into it, depending on the platform) and handle allocations and
deallocations yourself. And depending on the platform, this may or may
not be a good idea.
If you don't trust PHP to get it right in cases where it is documented
to work correctly, then you need to file a bug report with the developers.
If you STILL don't trust them to get it right, then why are you using
the language at all?
|
|
|
Re: I need Help [message #169712 is a reply to message #169707] |
Thu, 23 September 2010 19:19 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/23/2010 1:01 PM, Robert Hairgrove wrote:
> Jerry Stuckle wrote:
>> On 9/23/2010 3:56 AM, Robert Hairgrove wrote:
>>> Robert Hairgrove wrote:
>>>> Jerry Stuckle wrote:
>>>> > On 9/22/2010 4:46 PM, Robert Hairgrove wrote:
>>>> >> Patrick Ehrmann wrote:
>>>> >>> mysql_close ($verbindung);
>>>> >>
>>>> >> PHP takes care of this automatically, I believe.
>>>> >>
>>>> >
>>>> > Never a good idea to allow PHP to close the connection. Close it
>>>> > yourself when you're done!
>>>>
>>>> This is true for persistent connections. However:
>>>>
>>>> http://ch2.php.net/manual/en/function.mysql-close.php
>>>>
>>>> "(...) Using mysql_close() isn't usually necessary, as non-persistent
>>>> open links are automatically closed at the end of the script's
>>>> execution. See also freeing resources."
>>>
>>> Looking at some of the user comments, it seems that PHP applications on
>>> Windows connecting via TCP might have a problem in this regard. However,
>>> if you are connecting to the DB on localhost, e.g. on a Linux server
>>> hosting a website, then this shouldn't be a problem.
>>>
>>> From reading the rest of the problems posted there by users, looks like
>>> it is difficult to get it right -- best to let PHP take care of it
>>> unless there is a compelling reason otherwise.
>>
>> Except for extremely rare instances, you shouldn't use persistent
>> connections in PHP. They will almost always harm performance more than
>> they will help.
>>
>> And whether the script will close connections when it ends or not, it
>> is still not a good idea to depend on the system to close it for you.
>> This is true with all languages and all databases.
>>
>> Good programming practices means to always clean up after yourself and
>> don't depend on someone else to do it.
>
> Many other popular languages such as Java, C# and C++ also have what is
> known as a "garbage collection". With C++, of course, you can opt out of
> it (or into it, depending on the platform) and handle allocations and
> deallocations yourself. And depending on the platform, this may or may
> not be a good idea.
>
> If you don't trust PHP to get it right in cases where it is documented
> to work correctly, then you need to file a bug report with the developers.
>
> If you STILL don't trust them to get it right, then why are you using
> the language at all?
It has nothing to do with "trusting them to do it right". It has
everything to do with good programming practices.
You should never hold resources longer than you need them. Sure, they
will be cleaned up eventually. But the question is - WHEN? On a
heavily used system, it may take a relatively long time.
If you can't bother to use good programming practices such as this, I
would hate to see what the rest of your code looks like.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: I need Help [message #169717 is a reply to message #169693] |
Thu, 23 September 2010 19:44 |
Michael Fesser
Messages: 215 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
.oO(Beauregard T. Shagnasty)
> Jerry Stuckle wrote:
>
>> Robert Hairgrove wrote:
>>> Patrick Ehrmann wrote:
>>>> mysql_close ($verbindung);
>>>
>>> PHP takes care of this automatically, I believe.
>>
>> Never a good idea to allow PHP to close the connection. Close it
>> yourself when you're done!
>
> Rule 13:
> open the db connection
> do your query, your select, update, insert, whatever
> close the db connection
> proceed with other coding
I open the connection when my scripts initialize themselves and close it
when everything's done. I don't know beforehand which parts of my code
might need DB access for answering the current request and I don't want
to open/close the connection over and over again. So it stays open until
the end. In fact in a normal PHP script it doesn't make much of a
difference if you close the connection one or two milliseconds earlier.
Micha
|
|
|
Re: I need Help [message #169720 is a reply to message #169712] |
Thu, 23 September 2010 20:19 |
Robert Hairgrove
Messages: 19 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
Jerry Stuckle wrote:
> On 9/23/2010 1:01 PM, Robert Hairgrove wrote:
>> Jerry Stuckle wrote:
>>> On 9/23/2010 3:56 AM, Robert Hairgrove wrote:
>>>> Robert Hairgrove wrote:
>>>> > Jerry Stuckle wrote:
>>>> >> On 9/22/2010 4:46 PM, Robert Hairgrove wrote:
>>>> >>> Patrick Ehrmann wrote:
>>>> >>>> mysql_close ($verbindung);
>>>> >>>
>>>> >>> PHP takes care of this automatically, I believe.
>>>> >>>
>>>> >>
>>>> >> Never a good idea to allow PHP to close the connection. Close it
>>>> >> yourself when you're done!
>>>> >
>>>> > This is true for persistent connections. However:
>>>> >
>>>> > http://ch2.php.net/manual/en/function.mysql-close.php
>>>> >
>>>> > "(...) Using mysql_close() isn't usually necessary, as non-persistent
>>>> > open links are automatically closed at the end of the script's
>>>> > execution. See also freeing resources."
>>>>
>>>> Looking at some of the user comments, it seems that PHP applications on
>>>> Windows connecting via TCP might have a problem in this regard.
>>>> However,
>>>> if you are connecting to the DB on localhost, e.g. on a Linux server
>>>> hosting a website, then this shouldn't be a problem.
>>>>
>>>> From reading the rest of the problems posted there by users, looks like
>>>> it is difficult to get it right -- best to let PHP take care of it
>>>> unless there is a compelling reason otherwise.
>>>
>>> Except for extremely rare instances, you shouldn't use persistent
>>> connections in PHP. They will almost always harm performance more than
>>> they will help.
>>>
>>> And whether the script will close connections when it ends or not, it
>>> is still not a good idea to depend on the system to close it for you.
>>> This is true with all languages and all databases.
>>>
>>> Good programming practices means to always clean up after yourself and
>>> don't depend on someone else to do it.
>>
>> Many other popular languages such as Java, C# and C++ also have what is
>> known as a "garbage collection". With C++, of course, you can opt out of
>> it (or into it, depending on the platform) and handle allocations and
>> deallocations yourself. And depending on the platform, this may or may
>> not be a good idea.
>>
>> If you don't trust PHP to get it right in cases where it is documented
>> to work correctly, then you need to file a bug report with the
>> developers.
>>
>> If you STILL don't trust them to get it right, then why are you using
>> the language at all?
>
> It has nothing to do with "trusting them to do it right". It has
> everything to do with good programming practices.
>
> You should never hold resources longer than you need them. Sure, they
> will be cleaned up eventually. But the question is - WHEN? On a
> heavily used system, it may take a relatively long time.
>
> If you can't bother to use good programming practices such as this, I
> would hate to see what the rest of your code looks like.
>
This has nothing to do with my code. You are saying that the PHP docs
are wrong, that you know better, and following their advice is adamant
to bad programming practice.
So tell me this, why should I listen to you and ignore what the PHP
documentation says? Why should anyone listen to you?
*plonk*
|
|
|
Re: I need Help [message #169721 is a reply to message #169717] |
Thu, 23 September 2010 20:37 |
Beauregard T. Shagnas
Messages: 154 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
Michael Fesser wrote:
> .oO(Beauregard T. Shagnasty)
>> Jerry Stuckle wrote:
>>> Never a good idea to allow PHP to close the connection. Close it
>>> yourself when you're done!
>>
>> Rule 13:
>> open the db connection
>> do your query, your select, update, insert, whatever
>> close the db connection
>> proceed with other coding
>
> I open the connection when my scripts initialize themselves and close
> it when everything's done. I don't know beforehand which parts of my
> code might need DB access for answering the current request and I
> don't want to open/close the connection over and over again. So it
> stays open until the end. In fact in a normal PHP script it doesn't
> make much of a difference if you close the connection one or two
> milliseconds earlier.
Sure, if you have multiple db accesses, don't close it until the last
one is executed. That would be a given, of course. "Rule 13" was just a
basic guideline, stating the programmer should close the connection and
not allowing some timeout to handle it. ;-)
--
-bts
-Four wheels carry the body; two wheels move the soul
|
|
|
Re: I need Help [message #169722 is a reply to message #169720] |
Thu, 23 September 2010 21:59 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/23/2010 4:19 PM, Robert Hairgrove wrote:
> Jerry Stuckle wrote:
>> On 9/23/2010 1:01 PM, Robert Hairgrove wrote:
>>> Jerry Stuckle wrote:
>>>> On 9/23/2010 3:56 AM, Robert Hairgrove wrote:
>>>> > Robert Hairgrove wrote:
>>>> >> Jerry Stuckle wrote:
>>>> >>> On 9/22/2010 4:46 PM, Robert Hairgrove wrote:
>>>> >>>> Patrick Ehrmann wrote:
>>>> >>>>> mysql_close ($verbindung);
>>>> >>>>
>>>> >>>> PHP takes care of this automatically, I believe.
>>>> >>>>
>>>> >>>
>>>> >>> Never a good idea to allow PHP to close the connection. Close it
>>>> >>> yourself when you're done!
>>>> >>
>>>> >> This is true for persistent connections. However:
>>>> >>
>>>> >> http://ch2.php.net/manual/en/function.mysql-close.php
>>>> >>
>>>> >> "(...) Using mysql_close() isn't usually necessary, as non-persistent
>>>> >> open links are automatically closed at the end of the script's
>>>> >> execution. See also freeing resources."
>>>> >
>>>> > Looking at some of the user comments, it seems that PHP
>>>> > applications on
>>>> > Windows connecting via TCP might have a problem in this regard.
>>>> > However,
>>>> > if you are connecting to the DB on localhost, e.g. on a Linux server
>>>> > hosting a website, then this shouldn't be a problem.
>>>> >
>>>> > From reading the rest of the problems posted there by users, looks
>>>> > like
>>>> > it is difficult to get it right -- best to let PHP take care of it
>>>> > unless there is a compelling reason otherwise.
>>>>
>>>> Except for extremely rare instances, you shouldn't use persistent
>>>> connections in PHP. They will almost always harm performance more than
>>>> they will help.
>>>>
>>>> And whether the script will close connections when it ends or not, it
>>>> is still not a good idea to depend on the system to close it for you.
>>>> This is true with all languages and all databases.
>>>>
>>>> Good programming practices means to always clean up after yourself and
>>>> don't depend on someone else to do it.
>>>
>>> Many other popular languages such as Java, C# and C++ also have what is
>>> known as a "garbage collection". With C++, of course, you can opt out of
>>> it (or into it, depending on the platform) and handle allocations and
>>> deallocations yourself. And depending on the platform, this may or may
>>> not be a good idea.
>>>
>>> If you don't trust PHP to get it right in cases where it is documented
>>> to work correctly, then you need to file a bug report with the
>>> developers.
>>>
>>> If you STILL don't trust them to get it right, then why are you using
>>> the language at all?
>>
>> It has nothing to do with "trusting them to do it right". It has
>> everything to do with good programming practices.
>>
>> You should never hold resources longer than you need them. Sure, they
>> will be cleaned up eventually. But the question is - WHEN? On a
>> heavily used system, it may take a relatively long time.
>>
>> If you can't bother to use good programming practices such as this, I
>> would hate to see what the rest of your code looks like.
>>
>
> This has nothing to do with my code. You are saying that the PHP docs
> are wrong, that you know better, and following their advice is adamant
> to bad programming practice.
>
> So tell me this, why should I listen to you and ignore what the PHP
> documentation says? Why should anyone listen to you?
>
> *plonk*
I am saying there are a lot of things in the PHP documentation which do
not follow good programming practices, as developed by expert
programmers during over 50 years of programming. And PHP has done a lot
of stupid things over the years.
The people who wrote PHP are not experts in a lot of things. They are
just programmers. That has been shown time and time again.
But why try to talk to an idiot?
<plonk>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|