Re: Perl and Mysql display data problem
Martin MC Brown <[email protected]>
| Newsgroups | gmane.comp.db.mysql.perl |
|---|---|
| Message-ID | <[email protected]> |
Hi,
> Hello I have several problems. I'm trying to write a perl script
> that would
> check the username and password from Mysql database and allow users to
> login. I'm new to both systems and i have a lot of oruble with
> them. Here is
> the perl script that i'm trying to build.
OK, there are two main problems here, neither directly related to
MySQL. While your problems are not directly MySQL related, you may
have better luck (and faster responses) on one of the general Perl
lists.
That doesn't mean though that I'm not going to help while I can :)
The first is here:
> my ($input_username = "bobob", $input_password = "password")
You cannot assign default values in this way, you need to put the
variables on one side of an '=' and the values on the other, so the
line above becomes:
my ($input_username,$input_password) = ("bobob", "password");
> Global symbol "$input_username" requires explicit package name at ./
> login.pl
> line 34.
> Global symbol "$uid" requires explicit package name at ./login.pl
> line 40.
These two errors are related to your use of the 'strict' pragma:
use strict;
At the top of your script. Using this implies that all variables must
be declared before use, usually with either the 'my' or 'our' keyword.
In the script supplied, you've actually commented out the lines that
declare these variables appropriately.
> Another question i have, how to write the code so it could get for
> example
> first name and last name. and put them in to a hashed array, so i
> could
> display only fthe first or last name on the page.
I'm not quite sure what you are asking for here; you already
correctly extract the firstname and lastname into variables. To put
them in a hash you might use something like:
my %userdetails = ('firstname' => $fname,
'lastname' => $lname);
The '=>' operator is a synonym for ',', but makes the assignment here
clearer, with the hash key on the left of the operator, and the
corresponding value on the right. So you could access the values using:
$userdetails{'firstname'}
However, since you are loading the information from a database,
instead of using fetchrow_array() use fetchrow_hashref():
while (my ($row) = $sth->fetchrow_hashref())
{
...
}
Now you can access the fields from your query directly by name:
print "Firstname: $row->{fname}, lastname: $row->{lname}\n";
Hope this helps!
MC
--
Martin MC Brown, Technical Writer
MySQL AB, http://www.mysql.com
Skype: mcmcslp
--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/[email protected]