Code Comments
Programming Forum and web based access to our favorite programming groups.I would like to keep track of how many times each user logs in. When a user
fills in the form information on my login.html page it activates a PHP file
which.. well, here:
// BEGIN CODE //
<?php
session_start();
$auth = false; // Assume user is not authenticated
// Connect to MySQL
mysql_connect('localhost', 'username', 'password')
or die ( 'Unable to connect to server.' );
// Select database on MySQL server
mysql_select_db("dungeon_MemberDirectory")
or die ( 'Unable to select database.' );
// Formulate the query
$parameters = "SELECT * FROM Member WHERE
loginName = '$loginName' AND
password = '$password'";
// Execute the query and put results in $result
$result = mysql_query( $parameters )
or die ( 'Unable to execute query.' );
// Get number of rows in $result.
$num = mysql_numrows( $result );
if ( $num != 0 ) {
// A matching row was found - the user is authenticated.
$auth = true;
}
if ( ! $auth ) {
echo '<A HREF="java script:history.go(-1)">Go Back to Login</A><br><br>' ;
echo '<h3>Unable to login. Username and/or password invalid.</h3>';
exit;
}
else {
$_SESSION['auth']="true";
header("Location: thisw
.html");
}
?>
// END CODE //
The member table has a field called 'userCounter', which I would like to
update each time they log in. Right below $auth = true in my code, could I
insert something like
mysql_query(UPDATE Members SET userCounter = userCounter + 1 WHERE
loginName = '$loginName');
or does a mathematical operater not work in update and I need to take out
the userCounter before hand, add one to it, and then insert the new number?
Thanks in advance,
~Chris
Post Follow-up to this messageChris Martin wrote: > I would like to keep track of how many times each user logs in. When a > user fills in the form information on my login.html page it activates a [..] > > mysql_query(UPDATE Members SET userCounter = userCounter + 1 WHERE > loginName = '$loginName'); > > or does a mathematical operater not work in update and I need to take out > the userCounter before hand, add one to it, and then insert the new > number? [..] why you ask.. try.. and enjoy.. [..] > Thanks in advance, > > ~Chris -- www.iuz-lab.info
Post Follow-up to this message"iuz" <user@host.network> wrote in message news:0bhad.18288$b5.899335@news3.tin.it... > Chris Martin wrote: > > [..] out > [..] > > why you ask.. try.. and enjoy.. > > [..] > > -- > www.iuz-lab.info I ask for a few different reasons.. Firstly, I'd rather not inconvenience the users by messing up the web page accidently :). Secondly, I have a research paper to complete, presentations to design, other papers to write and a few books to read, and not much time to do it in. Posting a note to hear responses is a wonderful way to use my time, as I'll get feedback on not only if an idea would work, but also if there is a better way of going about doing it. Maybe the way I suggested would work but takes three times the processing time as some other way. Getting code to work usually isn't a problem for me.. it'll look nasty and going back to it later will not be fun, but it'll work. If I can discuss a better way of implementing my code in the design phase then downtime will be invisible to the users and I would have learned a better way to do something. ~Chris PS - I do understand what you are saying, though... over the wend I made a database system for a user interface using PHP and SQL, which I had never touched before, and had a wonderful time doing it.
Post Follow-up to this message"Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message news:pJidnYKN3P3wOvTcRVn-hw@adelphia.com... > "iuz" <user@host.network> wrote in message > news:0bhad.18288$b5.899335@news3.tin.it... > > I ask for a few different reasons.. Firstly, I'd rather not inconvenience > the users by messing up the web page accidently :). You shouldn't be doing that in a production environment. Do it in a test environment or on a test table. > Secondly, I have a > research paper to complete, presentations to design, other papers to write > and a few books to read, and not much time to do it in. And you could have executed that SQL statement (which you already wrote) in less time than it took to write these notes. With all you have to do, I would have taken the quickest route. > Posting a note to > hear responses is a wonderful way to use my time, as I'll get feedback on > not only if an idea would work, but also if there is a better way of going > about doing it. Well, that's a completely different issue. And you're too busy to spend time exploring code optimizations. > Maybe the way I suggested would work but takes three times > the processing time as some other way. Getting code to work usually isn't a > problem for me.. it'll look nasty and going back to it later will not be > fun, but it'll work. If I can discuss a better way of implementing my code > in the design phase then downtime will be invisible to the users and I would > have learned a better way to do something. You forgot... you're too busy for the discussions. (just messin' with ya) - Virgil
Post Follow-up to this messageWell... I am just turning my attention to PHP, but I have recently done
exactly the same thing in ASP for a visitorcounter in flash, which really
wouldn't make any difference since it in fact is a SQL question, and Yes,
your sql update works.just fine.
Too bad You haven't been able to get any answers except "try it", and "don't
do it in a production environment".
Not everybody have a test and development environment available.
/Jari
"Chris Martin" <christopher.martin@REMOVEadelphia.net> skrev i meddelandet
news:f7CdnRvZycLA4fTcRVn-pQ@adelphia.com...
>I would like to keep track of how many times each user logs in. When a
>user
> fills in the form information on my login.html page it activates a PHP
> file
> which.. well, here:
>
> // BEGIN CODE //
>
> <?php
> session_start();
> $auth = false; // Assume user is not authenticated
>
> // Connect to MySQL
> mysql_connect('localhost', 'username', 'password')
> or die ( 'Unable to connect to server.' );
>
> // Select database on MySQL server
> mysql_select_db("dungeon_MemberDirectory")
> or die ( 'Unable to select database.' );
>
> // Formulate the query
> $parameters = "SELECT * FROM Member WHERE
> loginName = '$loginName' AND
> password = '$password'";
>
> // Execute the query and put results in $result
> $result = mysql_query( $parameters )
> or die ( 'Unable to execute query.' );
>
> // Get number of rows in $result.
> $num = mysql_numrows( $result );
>
> if ( $num != 0 ) {
> // A matching row was found - the user is authenticated.
> $auth = true;
> }
>
> if ( ! $auth ) {
> echo '<A HREF="java script:history.go(-1)">Go Back to Login</A><br><br>' ;
> echo '<h3>Unable to login. Username and/or password invalid.</h3>';
> exit;
>
> }
> else {
> $_SESSION['auth']="true";
> header("Location: thisw
.html");
> }
>
> ?>
>
> // END CODE //
>
>
> The member table has a field called 'userCounter', which I would like to
> update each time they log in. Right below $auth = true in my code, could
> I
> insert something like
>
> mysql_query(UPDATE Members SET userCounter = userCounter + 1 WHERE
> loginName = '$loginName');
>
> or does a mathematical operater not work in update and I need to take out
> the userCounter before hand, add one to it, and then insert the new
> number?
>
> Thanks in advance,
>
> ~Chris
>
>
Post Follow-up to this messageTop posting cause it was my own post that I'm answering and I've already
read it...
Yes, the following code does actually work, in case anyone wanted to know.
mysql_query("UPDATE Member SET loginCounter = loginCounter + 1 WHERE
loginName = '$loginName'");
I was curious if inline addition was allowed using the update query, so
there is the answer, it does.
Next question is not a simple yes it does or no it doesn't answer. Is this
the most efficient method of achieving this. While in my mind I see one
command, I know it is going to be broken down into different steps.. so is
it more efficient to use a command to retrieve the value, then do the math
in the PHP code, then insert the new variable?
TIA,
~Chris
________________________________________
_____________________
"Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message
news:f7CdnRvZycLA4fTcRVn-pQ@adelphia.com...
> I would like to keep track of how many times each user logs in. When a
user
> fills in the form information on my login.html page it activates a PHP
file
> which.. well, here:
>
> // BEGIN CODE //
>
> <?php
> session_start();
> $auth = false; // Assume user is not authenticated
>
> // Connect to MySQL
> mysql_connect('localhost', 'username', 'password')
> or die ( 'Unable to connect to server.' );
>
> // Select database on MySQL server
> mysql_select_db("dungeon_MemberDirectory")
> or die ( 'Unable to select database.' );
>
> // Formulate the query
> $parameters = "SELECT * FROM Member WHERE
> loginName = '$loginName' AND
> password = '$password'";
>
> // Execute the query and put results in $result
> $result = mysql_query( $parameters )
> or die ( 'Unable to execute query.' );
>
> // Get number of rows in $result.
> $num = mysql_numrows( $result );
>
> if ( $num != 0 ) {
> // A matching row was found - the user is authenticated.
> $auth = true;
> }
>
> if ( ! $auth ) {
> echo '<A HREF="java script:history.go(-1)">Go Back to Login</A><br><br>' ;
> echo '<h3>Unable to login. Username and/or password invalid.</h3>';
> exit;
>
> }
> else {
> $_SESSION['auth']="true";
> header("Location: thisw
.html");
> }
>
> ?>
>
> // END CODE //
>
>
> The member table has a field called 'userCounter', which I would like to
> update each time they log in. Right below $auth = true in my code, could
I
> insert something like
>
> mysql_query(UPDATE Members SET userCounter = userCounter + 1 WHERE
> loginName = '$loginName');
>
> or does a mathematical operater not work in update and I need to take out
> the userCounter before hand, add one to it, and then insert the new
number?
>
> Thanks in advance,
>
> ~Chris
>
>
Post Follow-up to this message
"Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message
news:CbudnUH-fPwKlu7cRVn-vA@adelphia.com...
> Top posting cause it was my own post that I'm answering and I've already
> read it...
>
> Yes, the following code does actually work, in case anyone wanted to know.
>
> mysql_query("UPDATE Member SET loginCounter = loginCounter + 1 WHERE
> loginName = '$loginName'");
>
> I was curious if inline addition was allowed using the update query, so
> there is the answer, it does.
>
> Next question is not a simple yes it does or no it doesn't answer. Is
this
> the most efficient method of achieving this. While in my mind I see one
> command, I know it is going to be broken down into different steps.. so is
> it more efficient to use a command to retrieve the value, then do the math
> in the PHP code, then insert the new variable?
>
> TIA,
> ~Chris
It's the most atomic way. Your other suggestion leaves holes for concurrent
updates unless you are using proper commitment control.
- Virgil
Post Follow-up to this message"Jari Ivanoff" <jari.ivanoff@@comhem.se> wrote in message news:CkCcd.6432$d5.53660@newsb.telia.net... > Well... I am just turning my attention to PHP, but I have recently done > exactly the same thing in ASP for a visitorcounter in flash, which really > wouldn't make any difference since it in fact is a SQL question, and Yes, > your sql update works.just fine. Most of us knew that... but he won't learn to fend for himself if we just tell him that. > Too bad You haven't been able to get any answers except "try it", and "don't > do it in a production environment". Actually, it's good *precisely* because this is the kind of question that is best answered by experimentation and consultation with the manuals. > Not everybody have a test and development environment available. I'm interested in knowing the situation where someone can't have a test environment... at least enough to test the code in question. The software is free. The computers are already in place, obviously. So, other than an environment where *no* development was allowed, what would be the case in which something like this could not be tested? - Virgil
Post Follow-up to this message"Virgil Green" <vjg@DESPAMobsydian.com> wrote in message news:QZOcd.7216$Al3.7211@newssvr30.news.prodigy.com... > > "Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message > news:CbudnUH-fPwKlu7cRVn-vA@adelphia.com... know. > this is math > > It's the most atomic way. Your other suggestion leaves holes for concurrent > updates unless you are using proper commitment control. > > - Virgil > > Alright, thanks. I like to ask questions like I did instead of put a snippit of code into a test enviornment because I get people's views on the code as well. I knew I could get the code to work if I messed with it for 5 minutes, so I was more wondering if there was a better way of doing it that I was overlooking. Thanks again, ~Chris
Post Follow-up to this message"Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message news:o8edncGryaLsae7cRVn-1w@adelphia.com... > > "Virgil Green" <vjg@DESPAMobsydian.com> wrote in message > news:QZOcd.7216$Al3.7211@newssvr30.news.prodigy.com... already > know. so one so > is > math > concurrent > > Alright, thanks. I like to ask questions like I did instead of put a > snippit of code into a test enviornment because I get people's views on the > code as well. I knew I could get the code to work if I messed with it for 5 > minutes, so I was more wondering if there was a better way of doing it that > I was overlooking. Chris, I think the main point of (the drift of) this thread was that you will get much better responses here if you try it first, present your findings, and then ask for opinions regarding improvements in method/technique. - Virgil
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.