Problem with NOW() with PHP and MySQL
Anybody have an idea why (and I have been trying for 1 hour now) when I
execute this code that last_change is never update, but all the other
fields are?
I just have no idea!
$DB->query("UPDATE ".get_table_name('users')."
SET last_login=NOW(), last_change=NOW(),
session=?
WHERE user_id=?",
true, $_SESSION['user_id']);
Re: Problem with NOW() with PHP and MySQL
"Seansan" <sean115=at [at] =reeve.nl> wrote in message
news:460a952a$0$29129$dbd49001 [at] news.wanadoo.nl...
| Anybody have an idea why (and I have been trying for 1 hour now) when I
| execute this code that last_change is never update, but all the other
| fields are?
|
| I just have no idea!
|
| $DB->query("UPDATE ".get_table_name('users')."
| SET last_login=NOW(), last_change=NOW(),
| session=?
| WHERE user_id=?",
| true, $_SESSION['user_id']);
surely you write code following better standards of practice that this
spaghetti. i'm sure you just slung this slop here in said manner because it
is a ng and not your production source, right?
this would be more like what i'd guess your production source to look
like...right?
$stamp = date('Y-m-d H:i:s');
$userId = $_SESSION['user_id'];
$sql = "
UPDATE " . get_table_name('users') . "
SET last_login = '" . $stamp . "' ,
last_change = '" . $stamp . "'
WHERE user_id = '" . $userId . "'
";
$DB->query($sql);
as it is, i have a big problem with people who half-way decouple in-line sql
as you have done here. don't know what i mean? you are counting on the table
'users' to potentially have differing names however you then count on the
structure to always be the same...i.e. last_login and last_change. if you're
going to do it right, then decouple the whole damn thing. create a table
class. extend the class for each specific table. the table name will be used
to create the correct instance/version. the extended class knows the
structure of the table and will store all of your in-line sql. at that
point, you'd just be calling add/update/delete interfaces (passing the
necissary parameters to each) and it would do your $DB->query() calls. any
other measure is half-assed.
but that's just my 0.02 usd.
btw, i used $stamp for both fields being set as using NOW() (even if you got
your query to work) would set them to two different values even though the
transaction happened at the same time. further, i assume this is just a
login transaction...why on earth would last_change be set anyway - that
should relate to something they *changed* while logged on. don't you lose
valuable information that way once a user does log in?
this code speaks volumes to me even though you don't realize what it is
saying. to me, this is screaming 'i can be hacked easily because it appears
user information is only validated once and then stored in session data', 'i
don't really have any good kind of auditing system in place', and 'i really
could give two flying fucks about good coding practices'. don't care to hear
me warn you about all this? better me than your employer!
Re: Problem with NOW() with PHP and MySQL
Post removed (X-No-Archive: yes)
Re: Problem with NOW() with PHP and MySQL
On Mar 28, 1:03 pm, "Steve" <no.... [at] example.com> wrote:
> btw, i used $stamp for both fields being set as using NOW() (even if you got
> your query to work) would set them to two different values even though the
> transaction happened at the same time.
Are you sure about that? From <http://dev.mysql.com/doc/refman/4.1/en/
date-and-time-functions.html>:
<snip>
Functions that return the current date or time each are evaluated only
once per query at the start of query execution. This means that
multiple references to a function such as NOW() within a single query
always produce the same result.
</snip>
Re: Problem with NOW() with PHP and MySQL
Tom wrote:
> On Wed, 28 Mar 2007 18:17:19 +0200, Seansan wrote...
>> Anybody have an idea why (and I have been trying for 1 hour now) when I
>> execute this code that last_change is never update, but all the other
>> fields are?
>>
>> I just have no idea!
>>
>> $DB->query("UPDATE ".get_table_name('users')."
>> SET last_login=NOW(), last_change=NOW(),
>> session=?
>> WHERE user_id=?",
>> true, $_SESSION['user_id']);
>
> Maybe try building your statement as a variable, them print it out to make sure
> the syntax is correct.
>
> $cmd = ("UPDATE ".get_table_name('users')."
> SET last_login=NOW(), last_change=NOW(),
> session=?
> WHERE user_id=?";
>
> echo($cmd);
>
> $DB->query($cmd,true, $_SESSION['user_id']);
>
>
> Might also try using "mysql_error()" to see if that returns a specific error.
>
> Tom
> --
> Newsguy.com
> Express Accounts - 30 GB $9.95 / month
>
First of al thank you for your answers.
I just changed the table column to another name (now temporaily called
TEMP). Now it does update!!!
It seems like "last_change" is some kind of protected name/variable.....
or I dont know. BTW I use PEAR DB class.
I dont know what exactly is causing this, but I am resolving for now by
renaming the column so it will update.
Seansan
Re: Problem with NOW() with PHP and MySQL
> First of al thank you for your answers.
>
> I just changed the table column to another name (now temporaily called
> TEMP). Now it does update!!!
>
> It seems like "last_change" is some kind of protected name/variable.....
> or I dont know. BTW I use PEAR DB class.
>
> I dont know what exactly is causing this, but I am resolving for now by
> renaming the column so it will update.
>
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
The table is not a reserved word. However you should if at all possible
enclose table names in ticks (`) this would in the event of you using a
reserved word allow the sql to work.