Perl and Mysql display data problem

Perl and Mysql display data problem

am 25.06.2006 00:15:31 von FoRo

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.
------------------------------------------------------------ -----------------------------------------

#!/usr/bin/perl -wT

use strict;
use CGI;
use CGI::Carp;
use DBI;
use Template;


# Load the CGI input data.
my $html = new CGI;
print $html->header();

#my $dsn = 'DBI:mysql:ttf:localhost';
#my $db_user_name = 'web';
#my $db_password = 'nouser';
#my ($uid, $password);
#my $dbh = DBI->connect($dsn, $db_user_name, $db_password);


my ($sth);


my ($input_username = "bobob", $input_password = "password")


my $sth = $dbh->prepare(qq{
select uid, password from logins
where username = $input_username
});
$sth->execute();

($uid, $password) = $sth->fetchrow_array();
$sth->finish():
if ($input_password eq $password)
{
...
}

$sth->fetchrow_array()

my $sth = $dbh->prepare(qq{
select fname, lname from users
});
$sth->execute();
while (my ($fname, lname) =
$sth->fetchrow_array())
{
print "$fname, $lname\n";
}
$sth->finish();


my (@matrix) = ();
while (my @ary = $sth->fetchrow_array())
{
push(@matrix, [@ary]);
}
sth->finish();


$dbh->disconnect();

------------------------------------------------------------ -----------------------------------------

I keep on getting this errors, i have idea how to fix them. :)

------------------------------------------------------------ -----------------------------------------

syntax error at ./login.pl line 34, near ")


my "
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.
syntax error at ./login.pl line 41, near "):"
Execution of ./login.pl aborted due to compilation errors.

------------------------------------------------------------ -----------------------------------------


Any help would be really apreciated.

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.

Once again, thx guys.

--
View this message in context: http://www.nabble.com/Perl-and-Mysql-display-data-problem-t1 842429.html#a5029478
Sent from the MySQL - Perl forum at Nabble.com.


--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org

Re: Perl and Mysql display data problem

am 25.06.2006 03:42:23 von Darren Duncan

At 3:15 PM -0700 6/24/06, FoRo wrote:
>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.

Your problem is not specific to MySQL, but that you used some bad
general Perl syntax.

For example, the line that declares "my $uid" is commented out, so
you get that undeclared variable warning; you need to have an
uncommented "my $uid;".

Also, you should take this line:

my ($input_username = "bobob", $input_password = "password")

And change it to (note also the trailing semicolon):

my ($input_username, $input_password) = ("bobob", "password");

That explains the errors you are getting right now.

Assuming your Perl is version 5.6 or newer (or best, 5.8 or newer),
you can also put a "use warnings;" at the top of your script like the
"use strict;", which removes the need for any "-w" flag; easier to
use in my opinion.

-- Darren Duncan

--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org

Re: Perl and Mysql display data problem

am 25.06.2006 04:44:49 von Anthony Tang

------=_NextPart_000_0013_01C697DF.CCC2D6A0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hello,

I'm pretty experienced with using Perl and MySQL. Its what I do to =
build my web applications. For a authentication scheme, you may want to =
always check the username and password input. First do username and =
password validation before even calling mysql. For example, if your =
password supposed to have a minimum of 6 characters, then validate that =
before you connect to your MySQL database.

Also, you should look into using MD5 for your password column for your =
user database.

As for your Perl Syntax problem....perhaps chang this part of your =
coding:

($uid, $password) =3D $sth->fetchrow_array();
$sth->finish(): #<---CHANGE : to ; =20
if ($input_password eq $password)=20
{
...
}


----- Original Message -----=20
From: "FoRo"
To:
Sent: Saturday, June 24, 2006 6:15 PM
Subject: Perl and Mysql display data problem


> 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.
> =
------------------------------------------------------------ -------------=
----------------------------
>=20
> #!/usr/bin/perl -wT
>=20
> use strict;
> use CGI;
> use CGI::Carp;
> use DBI;
> use Template;
>=20
>=20
> # Load the CGI input data.
> my $html =3D new CGI;
> print $html->header();
>=20
> #my $dsn =3D 'DBI:mysql:ttf:localhost';
> #my $db_user_name =3D 'web';
> #my $db_password =3D 'nouser';
> #my ($uid, $password);
> #my $dbh =3D DBI->connect($dsn, $db_user_name, $db_password);
>=20
>=20
> my ($sth);
>=20
>=20
> my ($input_username =3D "bobob", $input_password =3D "password")=20
>=20
>=20
> my $sth =3D $dbh->prepare(qq{
> select uid, password from logins
> where username =3D $input_username
> });
> $sth->execute();
>=20
> ($uid, $password) =3D $sth->fetchrow_array();
> $sth->finish():=20
> if ($input_password eq $password)=20
> {
> ...
> }
>=20
> $sth->fetchrow_array()
>=20
> my $sth =3D $dbh->prepare(qq{
> select fname, lname from users
> });
> $sth->execute();
> while (my ($fname, lname) =
> $sth->fetchrow_array())
> {
> print "$fname, $lname\n";
> }
> $sth->finish();
>=20
>=20
> my (@matrix) =3D ();
> while (my @ary =3D $sth->fetchrow_array())
> {
> push(@matrix, [@ary]);
> }
> sth->finish();
>=20
>=20
> $dbh->disconnect();
>=20
> =
------------------------------------------------------------ -------------=
----------------------------
>=20
> I keep on getting this errors, i have idea how to fix them. :)
>=20
> =
------------------------------------------------------------ -------------=
----------------------------
>=20
> syntax error at ./login.pl line 34, near ")
>=20
>=20
> my "
> 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.
> syntax error at ./login.pl line 41, near "):"
> Execution of ./login.pl aborted due to compilation errors.
>=20
> =
------------------------------------------------------------ -------------=
----------------------------
>=20
>=20
> Any help would be really apreciated.
>=20
> 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.
>=20
> Once again, thx guys.
>=20
> --
> View this message in context: =
http://www.nabble.com/Perl-and-Mysql-display-data-problem-t1 842429.html#a=
5029478
> Sent from the MySQL - Perl forum at Nabble.com.
>=20
>=20
> --=20
> MySQL Perl Mailing List
> For list archives: http://lists.mysql.com/perl
> To unsubscribe: =
http://lists.mysql.com/perl?unsub=3DAnthony.Tang@Contrarian- Investments.c=
om
>=20
>=20
>=20
>=20
>=20
> --=20
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.394 / Virus Database: 268.9.3/374 - Release Date: =
6/23/2006
>=20
>
------=_NextPart_000_0013_01C697DF.CCC2D6A0--

Re: Perl and Mysql display data problem

am 25.06.2006 09:01:28 von Martin.Evans

Others have replied about your syntax problem but you've got
other problems you don't know yet.

> my $sth = $dbh->prepare(qq{
> select uid, password from logins
> where username = $input_username
> });

The username field is not quoted (see DBI docs for quote method) but
better still use parameters:

$sql = q/select uid,password from logins where username = ?/;
$sth->prepare($sql);
$sth->execute($input_username);

or you will be susceptible to sql injection.

Also, since you know the username and password, why read the password
and test it - why not let the database do it:

select uid from logins where username = ? and password = ?

Lastly, your fetchrow array to populate @matix is virtually
the builtin method fetchall_arrayref.

Martin

FoRo wrote:
> 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.
> ------------------------------------------------------------ -----------------------------------------
>
> #!/usr/bin/perl -wT
>
> use strict;
> use CGI;
> use CGI::Carp;
> use DBI;
> use Template;
>
>
> # Load the CGI input data.
> my $html = new CGI;
> print $html->header();
>
> #my $dsn = 'DBI:mysql:ttf:localhost';
> #my $db_user_name = 'web';
> #my $db_password = 'nouser';
> #my ($uid, $password);
> #my $dbh = DBI->connect($dsn, $db_user_name, $db_password);
>
>
> my ($sth);
>
>
> my ($input_username = "bobob", $input_password = "password")
>
>
> my $sth = $dbh->prepare(qq{
> select uid, password from logins
> where username = $input_username
> });
> $sth->execute();
>
> ($uid, $password) = $sth->fetchrow_array();
> $sth->finish():
> if ($input_password eq $password)
> {
> ...
> }
>
> $sth->fetchrow_array()
>
> my $sth = $dbh->prepare(qq{
> select fname, lname from users
> });
> $sth->execute();
> while (my ($fname, lname) =
> $sth->fetchrow_array())
> {
> print "$fname, $lname\n";
> }
> $sth->finish();
>
>
> my (@matrix) = ();
> while (my @ary = $sth->fetchrow_array())
> {
> push(@matrix, [@ary]);
> }
> sth->finish();
>
>
> $dbh->disconnect();
>
> ------------------------------------------------------------ -----------------------------------------
>
> I keep on getting this errors, i have idea how to fix them. :)
>
> ------------------------------------------------------------ -----------------------------------------
>
> syntax error at ./login.pl line 34, near ")
>
>
> my "
> 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.
> syntax error at ./login.pl line 41, near "):"
> Execution of ./login.pl aborted due to compilation errors.
>
> ------------------------------------------------------------ -----------------------------------------
>
>
> Any help would be really apreciated.
>
> 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.
>
> Once again, thx guys.
>
> --
> View this message in context: http://www.nabble.com/Perl-and-Mysql-display-data-problem-t1 842429.html#a5029478
> Sent from the MySQL - Perl forum at Nabble.com.
>
>


--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org

Re: Perl and Mysql display data problem

am 25.06.2006 10:24:10 von Martin MC Brown

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/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org