User Control Panel
Advertisements

HELP US, HELP YOU!

I got it to return but....

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl
View unanswered posts
Author Message
xiloki
Newbie
Newbie


Joined: 28 Mar 2005
Posts: 12
Location: pa
Reputation: 13.8

PostPosted: Thu Mar 31, 2005 9:22 pm    Post subject: Reply with quote

Thanks for all your help. I got it to return a value to the command window. But all it returns is 1. Is that the row? Why am I so stupid with this fetchrow command? Ideas?




Code:
<br />           #connect to db<br />      my $dbh = DBI->connect("DBI:mysql:<database>:<host>","<user>","<pass>");<br />        <br />      #retrieve clients info from db<br />      #my $query = "select * from users";<br />      my $sth = $dbh->prepare("SELECT name FROM users where sn = ?");<br />      $sth->execute($client);<br />      <br />      my @user_array = $sth->fetchrow();    <br />         print @user_array . "\n";<br />      <br />      $sth->finish();<br />      $dbh->disconnect();<br />
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Thu Mar 31, 2005 10:07 pm    Post subject: Reply with quote

Actually, I think it might have been my mistake in the example. print @array; will print all the items in the array delimited by single spaces. But with print @array . "\n", I believe it prints the number of items in the array.

Try this:

Code:
my $count = scalar @array;<br />print( "Found $count (" . join( ", ", @array ) . ")\n" );
Back to top
xiloki
Newbie
Newbie


Joined: 28 Mar 2005
Posts: 12
Location: pa
Reputation: 13.8

PostPosted: Thu Mar 31, 2005 11:07 pm    Post subject: Reply with quote

I want it to print all the items in the array.
Code:
<br />           #connect to db<br />      my $dbh = DBI->connect("DBI:mysql:<database>:<host>","<user>","<pass>");<br />        <br />      #retrieve clients info from db<br />      #my $query = "select * from users";<br />      my $sth = $dbh->prepare("SELECT name FROM users where sn = ?");<br />      $sth->execute($client);<br />      <br />      my @user_array = $sth->fetchrow();    <br />          print @user_array;<br />      <br />      $sth->finish();<br />      $dbh->disconnect();<br />


But this produces no output in the cmd window.
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Fri Apr 01, 2005 12:37 am    Post subject: Reply with quote

Are you sure it is connecting to the db correctly? Also, what is the value of $client? I don't see it defined here. Usually in cases like this, I remove all extraneous data, variables, etc and put print statements (unless you have a debugger of course) throughout the code to see how far it is getting and what values it has:

Code:
       #connect to db<br />     my $dbh = DBI->connect("DBI:mysql:<database>:<host>","<user>","<pass>");<br />     print( "dbh is good\n") if( defined $dbh );<br /><br />     my $sth = $dbh->prepare("SELECT name FROM users LIMIT 1");<br />     print( "sth is good\n") if( defined $sth );<br /><br />    $sth->execute();<br />     <br />     my @user_array = $sth->fetchrow();    <br />         print @user_array;<br />     <br />     $sth->finish();<br />     $dbh->disconnect();


Something like that. Also note that since you are only getting one field (name) from the table, it will only print one item. Obvious, but you were saying you want it to pring all of the items, which will just be 1.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group