Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need two lists printed from a MySql database. Why doesn't this work?

Status
Not open for further replies.

cradletoenslave

Programmer
Jan 28, 2005
29
US
Code:
while (my ($firstname) = 
    $sth->fetchrow_array())
{
     print "$firstname<br>";
}

$sth->finish();

while (my ($email) = 
    $sth->fetchrow_array())
{
     print "$email<br>";
}

$sth->finish();
 
We aren't dentists.

Post all relevent portions of the script, error messages, versions, module versions, etc...

--
Andy
&quot;Historically speaking, the presence of wheels in Unix has never precluded their reinvention.&quot;
Larry Wall
 
Sorry, I thought it was more straightforward than that. My bad.

I'm using the DBI module. I don't get any error messages, interesting enough. It just won't display on the page. It displays the first names just fine but it won't print the email list. Weird huh?

Have I supplied enough information?
 
The script. Especially, but not limited to, the sql.

--
Andy
&quot;Historically speaking, the presence of wheels in Unix has never precluded their reinvention.&quot;
Larry Wall
 
This part all works just fine.
Code:
#!/usr/bin/perl -Tw

require "/home/jaredmg/public_html/cgi-bin/formprocessor.cgi";
&ReadParse (*input);

$name= $input{'name'};
$codeemail= $input{'email'};



use DBI;
$dbh = DBI->connect("DBI:mysql:jaredmg_form:localhost","jaredmg_jaredmg","springer");
$dbh->do("insert into form 
  (firstname, email) values ('$name', '$codeemail')");

my $sth = $dbh->prepare(qq{
    select firstname, email from form
});
$sth->execute();
print "content-type: text/html\n\n";
print "<html><body>";
while (my ($firstname) = 
    $sth->fetchrow_array())
{
     print "$firstname<br>";
}

$sth->finish();
This part does not.
Code:
while (my ($email) = 
    $sth->fetchrow_array())
{
     print "$email<br>";
}

$sth->finish();

print "</body></html>";

Everything is being put into the database just fine. I checked it up with PHPmyadmin and everything that is entered from the form goes in. I just can't get it to display the email field.

The site can be checked here:


That will take you to the form.

Any help is greatly appreciated.
 
Can you see the variable names:

$name= $input{'name'};
$codeemail= $input{'email'};

#************************************
print "NAME:$name,EMAIL=$$codeemail\n";

#*************************************

use DBI;
$dbh = DBI->connect("DBI:mysql:jaredmg_form:localhost","jaredmg_jaredmg","springer");
$dbh->do("insert into form
(firstname, email) values ('$name', '$codeemail')");

my $sth = $dbh->prepare(qq{
select firstname, email from form
});
$sth->execute();
print "content-type: text/html\n\n";
print "<html><body>";





dmazzini
GSM System and Telecomm Consultant

 
Wouldn't that just print what they typed in? I need everything printed that is in the MySql field.
 
Yes I knew ir, but I wanted to be sure that you were receiving the right input values.

So now checking the Database..

Could you try:

my $sql = "select firstname, email from form";
my $sth = $dbh->prepare($sql);
$sth->execute;

while (@row = $sth->fetchrow_array) {
print "$row[0],$row[1]\n";
}
$sth->finish();


Yo should be able to see all your records. Please post the output.

dmazzini
GSM System and Telecomm Consultant

 
FYI, for future reference, it is generally not a good idea to post your password in public forums.

You really need to trash this code and start again.

First, use CGI to parse forms.

Second, do not trust user input. If you allow the user to insert any text they want in a form field and don't bother to check it, there is nothing to stop them from entering:

sometext'; drop database jaredmg_form;

So they close the sql statement with a ' and ;, then they can put whatever other sql statements they want.

Please have a look at Ovid's course on CGI here: [URL unfurl="true"]http://users.easystreet.com/ovid/cgi_course/[/url]

--
Andy
&quot;Historically speaking, the presence of wheels in Unix has never precluded their reinvention.&quot;
Larry Wall
 
Oh my, I didn't even think about the password in the script! Thanks for the advice.

Thanks for the link, I'll look into it.
 
cradletoenslave

It looks like you run your SQL statement which opens a cursor on the result set. You iterate over the results, printing the names. Then you finish, (a redundant statement as the fetch of the last row from the cursor should do this automatically). Then you try to iterate over the now-cursor again to print the second list. Not surprisingly, it doesn't work.

Either re-execute the statement (quick, dirty, crap design, poor performance) or rework your code to use
Code:
$sth->fetchall_arrayref()
then iterate over the resulting arrays to print your data.
 
stevexff - I agree, this is why I wrote a module to get the SQL and place into a record set (Array of Hashes) - you can then loop over, play around with and display the data as many times as you like without keep having to re-open a connection and re-query the SQL.

Ok you have the overhead of storing all records as an array, but PERL seems more than man enough for the job so far :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top