Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsDiscussion Forum → PHP & MySQL help
PHP & MySQL help
2007-02-27, 1:14 PM #1
This simple script should compare the given credentials against an entry in the database. If a match is found, it sets session information and updates the current time as being the user's last log-in time.

However, the update isn't working. It's valid SQL, because I can plug it into phpMyAdmin, and it works dandilicous.

:wtc:

Code:
<?PHP

        session_start();

        # Connect to the DB
        include('.databasehandler.php');

        # Get the passed POST information
        $username = mysql_real_escape_string(stripslashes($_POST['username']));
        $password = MD5(stripslashes($_POST['password']));

        $query = "SELECT userID, lastLogin FROM users WHERE username = '$username' AND pwd = '$password'";
        $result = mysql_query($query, $link);

        # If there was a result, log the user in
        if (mysql_num_rows($result) == 1) {

                $_SESSION['userame'] = stripslashes($username);
                $_SESSION['userID'] = intval(mysql_result($result, 0, 0));
                $_SESSION['lastLogin'] = intval(mysql_result($result, 0, 1));

                # Now, update the user's last login to be the current time
                $query = "UPDATE users SET lastLogin = ".time()." WHERE userID = ".intval($_SESSION['userID']);
                mysql_query($query, $link);

                # ***** and moan if the update didn't work
                if (mysql_affected_rows($link) != 1) { echo $query; echo "<br />"; echo "ERROR!"; }

        }

        else {

                $_SESSION['error'] = "ERROR:  No such username/password combination found.";

        }

        mysql_close($link);

        header("Location: ".$_SERVER['HTTP_REFERER']);
        exit();

?>
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-27, 1:36 PM #2
Have you tried?
Code:
$query=sprintf("UPDATE users SET lastLogin='%s' WHERE userID=%d",
   time(),intval($_SESSION['userID']));


I find the use of sprintf better because of easier to read then a bunch of concatenations.
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-02-27, 1:43 PM #3
What about using the mysql NOW() function?

$query = "UPDATE users SET lastLogin = NOW() WHERE userID = ".intval($_SESSION['userID']);

Also, I don't know if it is intentional, but your $_SESSION['username'] is spelt userame.
2007-02-27, 1:47 PM #4
I hurriedly changed various table and variable names because I'm a paranoid son of a *****.

The query is structured just fine - plugging it directly into MySQL works dandily. It seems to be that mysql_query() function that's not executing.

I changed the query to:

UPDATE users SET lastLogin = UNIX_TIMESTAMP(NOW()) WHERE userID = 1

The PHP still errored out, but the SQL was fine. :confused:
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-27, 2:04 PM #5
Without knowing the PHP error, are you sure the msql account you are connecting with has UPDATE permissions for the table?
2007-02-27, 2:45 PM #6
Neither MySQL nor PHP throw any errors - without that if (mysql_affected_rows()) check, the PHP continues on without interruption (though the update does not work).

The user has insert, select, update, and delete permissions.

[ Edit - never mind. Apparently my update permissions got removed. :confused: I really wish CPanel had a better way to track database user privileges ]
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-27, 3:28 PM #7
Hehe, thought so. Kinda odd it wouldn't generate an error though.

I always use PHPMyAdmin to manage privileges for that exact reason.
2007-02-27, 6:14 PM #8
ASP.NET > PHP

I WENT THERE!
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-02-27, 10:24 PM #9
Is it? Lemme install .NET 2.0 on my Kubuntu machine here and

oh wai
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-27, 10:28 PM #10
Originally posted by JediGandalf:
ASP.NET > PHP

I WENT THERE!


my butt > PHP

(still not saying much)
2007-02-27, 11:13 PM #11
Originally posted by Wolfy:
Is it? Lemme install .NET 2.0 on my Kubuntu machine here and

oh wai

Mono project FTW

(but they have ASP.NET 1.1...well they do have some of 2.0)
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-02-28, 6:43 AM #12
If and when they get 2.0 support that isn't described as "shaky", I'll try and maybe - maybe - install it. But God knows that Wine is such a terrific product, and I'm sure Mono will be exactly the same. :downswords:

Brian: you'd rather use Perl for web-scripting? Because that's just plain silly, considering it's a shell scripting language, and PHP has much better support for web session management as well as a library of built-in web-related functions that are far-better documented than Perl's are.
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-28, 7:15 AM #13
JSP > PHP
Detty. Professional Expert.
Flickr Twitter
2007-02-28, 7:41 AM #14
What does JSP bring to the table that PHP doesn't?
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-28, 8:02 AM #15
Originally posted by Wolfy:
If and when they get 2.0 support that isn't described as "shaky", I'll try and maybe - maybe - install it. But God knows that Wine is such a terrific product, and I'm sure Mono will be exactly the same. :downswords:

Brian: you'd rather use Perl for web-scripting? Because that's just plain silly, considering it's a shell scripting language, and PHP has much better support for web session management as well as a library of built-in web-related functions that are far-better documented than Perl's are.


Don't you know that hating whatever is popular makes you cool?
2007-02-28, 8:23 AM #16
JSP brings you:
Type Safety
Compiled classes (faster)
A lot of decent frameworks like Struts, Tapestry, Spring and Hibernate.
The huge Java API

ASP.NET technically allows the same if you use C#, but the standard approach of ASP is ugly. But JSP can also be run on any platform already.
Detty. Professional Expert.
Flickr Twitter
2007-02-28, 8:46 AM #17
But JSP has approximately nil support in webservers...
2007-02-28, 8:54 AM #18
There are plenty of JSP webservers like Tomcat and JBoss, and they can be run through Apache with a little bit of tweaking.

JSP is also used by a huge number of businesses, it's basically essentially if you want to get a job in web development.
Detty. Professional Expert.
Flickr Twitter
2007-02-28, 9:07 AM #19
Strange that I don't ever even hear anything about it if it's in such "popular" usage.

http://www.interaktonline.com/Support/Articles/Details/?id_art=11

Seems that although big businesses might stick with JSP or ASP, PHP is easily a contender, especially in the small/medium business arena.

One can write a type-safe script without a type-safe language anyway...

PHP has many caching plugins for it to simulate the "compiled" state of JSP...

And Java API? I'd rather not think of that :p
2007-02-28, 9:10 AM #20
When I've interviewed with companies like AT&T, Cerner (medical software company), and the like, they use a combination of J2EE, JSP, and Oracle.
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-28, 9:21 AM #21
Which is what I said, except for big businesses :p
2007-02-28, 9:23 AM #22
I applied for a bunch of small-large sized companies (10-5000) companies. None of them used PHP, a few used ASP and most used JSP.

There is a huge inconsistancy between what hobbyists and freelancers use compared to what the companies use. The only companies that really use PHP are those that started as hobbyists and turned into small startups.

Yes, you can write a type-safe script without a type-safe language, you can also write a program without errors without doing any error-handling whatsoever. You'd just be insane to try.
Detty. Professional Expert.
Flickr Twitter
2007-02-28, 9:41 AM #23
Originally posted by Cool Matty:
Which is what I said, except for big businesses :p


Cerner's big in Missouri, but I wasn't aware it was big in the way a company such as AT&T is big.
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-28, 12:14 PM #24
Originally posted by Wolfy:
Brian: you'd rather use Perl for web-scripting? Because that's just plain silly, considering it's a shell scripting language, and PHP has much better support for web session management as well as a library of built-in web-related functions that are far-better documented than Perl's are.


Actually it's not silly. mod_perl is faster than php, it's object model, while obtuse, is not completely broken. References are implemented in a sane manner in perl. Perl is not a "shell scripting" language, and even if it is, programming for the web is stateless and deals with processing text. Through CPAN, there are literally thousands of modules you can use for free, even in commercial, distributed applications (they are not GPL but perl artistic license which permits redistribution). Including dozens for session management, which, by the way, is not a complex or difficult problem to solve in the first place. Through CPAN, perl also has some of the most robust and well-tested modules for working on web-related problems.

Perl's major problem is how complex the syntax can be (although it doesn't have to be, it often is because perl programmers generally like to be confusing). Another problem, although not perl's problem directly, is that php script kiddies constantly bring up the argument that perl wasn't "designed for the web" and is therefore somehow inferior to PHP, which supposedly was "designed for the web."

The fundamental issue is that "the web" is simply a series of HTTP requests and responses which contain text markup. I would choose python, ruby, and perl over PHP for web development in a heartbeat. And I have more years of experience with PHP than I have with all the others combined. The more I use PHP, the more I hate it. The more brain-dead decisions the php devs make, the more I hate it. Every time I code in PHP I run into yet another problem without a decent fakking solution. Coding "easy" stuff in PHP is nice. But they make the "hard" stuff damn-near impossible. Whereas with perl, the easy stuff is easy, and the "hard" stuff is possible but challenging.

Do some research on mod_perl, either you'll be blown away at the power, or you'll completely misunderstand the point ;)

On the other hand, if I were going to distribute a a web application (something like vbulletin), I would probably choose PHP and MySQL, even though I hate both of them. I would choose them because the vast majority of hosting providers support PHP & MySQL out of the box. Perl support is generally there as well, but it's often older versions or stripped-down versions with no access to CPAN (which is moronic).
2007-02-28, 12:27 PM #25
You hate MySQL? What else would you use? Postgres? Oracle?
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-02-28, 12:37 PM #26
MsSQL! :v:
2007-02-28, 12:55 PM #27
Originally posted by JediGandalf:
You hate MySQL? What else would you use? Postgres? Oracle?


Well, I guess in this case, Hate is a strong word. I don't hate MySQL. In earlier versions, it lacked features that make a "real" database - transactions and referential integrity constraints in particular. Stored procedures. It also handles "NOT NULL" constraints in a brain-dead manner (if you try to insert a NULL into a NOT NULL column, instead of rejecting input, it magically translates it into an empty string or a 0 or whatever else will fit in the column).

I like Postgres for the most part, but of course it has warts of its own. I program against pg and oracle every day at work, and I detest Oracle for so many reasons.

I would definitely choose Pg if I had a choice. The open source firebird looks pretty cool, too, but I've never used.

Out of pg, oracle, solid, and mysql, I would use them in this order:

pg
mysql
oracle
solid
2007-02-28, 12:57 PM #28
Originally posted by Brian:
Every time I code in PHP I run into yet another problem without a decent fakking solution. Coding "easy" stuff in PHP is nice. But they make the "hard" stuff damn-near impossible. Whereas with perl, the easy stuff is easy, and the "hard" stuff is possible but challenging.


Can you come up with some examples?
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-28, 1:17 PM #29
I'm told that MySQL is optimized for web transactions quite well where as Postgres not so much. Confirm/deny?

Also, didn't MySQL 5 make significant improvements?
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-02-28, 1:39 PM #30
Originally posted by Brian:
Well, I guess in this case, Hate is a strong word. I don't hate MySQL. In earlier versions, it lacked features that make a "real" database - transactions and referential integrity constraints in particular. Stored procedures. It also handles "NOT NULL" constraints in a brain-dead manner (if you try to insert a NULL into a NOT NULL column, instead of rejecting input, it magically translates it into an empty string or a 0 or whatever else will fit in the column).


MySQL will reject any insertions of NULL into a NOT NULL column; on top of that, though, your DB app should be checking the input values before it tries to insert them.

MySQL also supports functions, triggers, views, and nested select queries.
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-28, 3:50 PM #31
SQL isn't a proper relational language anyway.
Detty. Professional Expert.
Flickr Twitter
2007-03-01, 10:43 AM #32
Originally posted by Wolfy:
MySQL will reject any insertions of NULL into a NOT NULL column; on top of that, though, your DB app should be checking the input values before it tries to insert them.

MySQL also supports functions, triggers, views, and nested select queries.


Not in the versions I have been using. I started with 3.23 and I think I even ran into this in 4. I haven't actively used 5 yet. I don't have time to wait around for them to code features that Pg has had for years.
2007-03-01, 10:47 AM #33
Originally posted by JediGandalf:
I'm told that MySQL is optimized for web transactions quite well where as Postgres not so much. Confirm/deny?

Also, didn't MySQL 5 make significant improvements?


But what does that really mean? Transactions in the RDBMS sense of the word, allow you to specify that all queries must either succeed or they will all be rejected and changes rolled back. That way, if you have to run 5 queries, and the final one fails, your application doesn't have to roll back all those queries manually, but rather, the db does it for you. This is very important in many applications, scientific, financial, etc. It can also be a help in things like bulletin boards because you won't get a bunch of orphan data when things go wrong. It also allows you to write a LOT less code because you know the db can handle it if there's an error in the transaction - you don't have to constantly verify whether the data was put in properly.

MySQL made tons of good changes, but once you use the table types that have these features, the performance difference between it and Pg no longer exists. So if you need the features that make up a real database, picking MySQL gets you no benefit performance-wise.

If, like most people, you use MySQL as a glorified file system and thus don't use the table types that support transactions, referential integrity constraints, etc., you will get better performance but your application code has to be more complex.
2007-03-01, 10:52 AM #34
Originally posted by Wolfy:
Can you come up with some examples?


Sure.

Make a webdav server in PHP.
Make a webdav client in PHP.
Build up a bunch of objects, and then store references to them in an array, and you have to constantly type = & $obj over and over or the fact that you are a reference and not a copy disappears. (php4)
Or in PHP5, try to make a copy by saying $obj2 = $obj1 and it's impossible, you have to use special methods to copy an object - references in PHP, both 4 and 5, are completely brain-dead. C/C++/perl has the right idea, once you are holding a reference or pointer to something, it's always a reference or pointer until you explicitly dereference it.

There are so many cool things you can do with mod_perl and apache - authentication handlers that require authorization to view any page, not just other perl/php pages which have the code "included" at the top. Cleanup handlers, shared memory, a decent fakking unified DB layer with parameter placeholders, automatic and reliable escaping of data passed through said placeholders, the complete inability to do sql injection attacks unless you are a complete moron and don't use sql placeholders.

Perl also has awesome taint checking which ensures at the compiler/interpreter level that you are not using tainted data in any way - completely AWESOME for web development.
2007-03-01, 10:59 AM #35
Also, perl seems to be a lot more stable. In the past few years, I've run into about a half-dozen bugs in PHP itself (which were patched in later releases, but it seriously halts development or makes you do stupid stuff to workaround the problem - especially since if you distribute your app, you have to make sure all your users have php version > the one with the bug).

↑ Up to the top!