User Control Panel
Advertisements

HELP US, HELP YOU!

fetchrow

 
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 2:00 am    Post subject: Reply with quote

I keep getting the error :
can't coerce arrary into hash in on_im.pl on line 34
I'm trying to retireve data from a mysql db with the fields:id, sn, name, website

Code:
<br /><br /><br />use DBI;<br />use MySql;<br />use Strict;<br /><br />sub on_im{<br />   my ($aim,$client,$msg,$away) = @_;<br />   <br /><br />  #filter message for html<br />  $msg =~ s/<(.|\n)+?>//g;  <br />  <br />  #print im to cmd window<br />  print "[$client] $msg\n\n";<br />  <br />  #send the typing status to client<br />  $aim->send_typing_status ($client,TYPINGSTATUS_STARTED);<br />  <br />  #sleep for rate limit<br />  sleep(3);<br />  <br />           #connect to db<br />      my $dbh = DBI->connect("DBI:mysql:<database>:<host>","<user>","<password>");<br />        <br />      #retrieve clients info from db<br />      my $query = "select name from users where sn = ?";<br />      my $sth = $dbh->prepare($query);<br />      $sth->execute($client);<br />      <br />      while ( @user_array = $sth->fetchrow_hashref()) {       <br />      print @user_array->{"name"};<br />      }<br />      $sth->finish();<br />      $dbh->disconnect();<br />      <br />  #do work<br /><br />  <br />     #send im<br />  #$aim->send_im($client,$reply);<br />  <br />  #print response to im window<br />  print "[$screenname] $reply \n";<br />  <br />  #stop the typing status<br />  $aim->send_typing_status ($client, TYPINGSTATUS_FINISHED);<br /><br />   <br />}<br />1;<br />


Ideas? Comments? Wanna yell at me?

Whatever, I just need to get this working..lol
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Thu Mar 31, 2005 3:24 am    Post subject: Reply with quote

Quote:
while ( @user_array = $sth->fetchrow_hashref()) {   
     print @user_array->{"name"};
}


I assume fetchrow_hashref returns a hash reference and not an array. Also, @user_array->{"name"} is totally illegal. You use arrow notation (->) on references, not arrays. It's been a while since I dealt directly with the low level DBI calls, but I think you're looking for this:

Code:
while ( my $hashref = $sth->fetchrow_hashref()) {    <br />     print $hashref->{"name"};<br />}
Back to top
xiloki
Newbie
Newbie


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

PostPosted: Thu Mar 31, 2005 8:25 am    Post subject: Reply with quote

Thanks. That did it. I'm not familar with perl and perl syntax... I've programmed in c and java and i know php, html etc.
I don't understand why I'm having such an issue learning perl.
Anyways I no longer get that error but it returns a blank im. Any ideas?
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Thu Mar 31, 2005 8:52 am    Post subject: Reply with quote

I come from a C background as well and using the punctuation that Perl requires in front of variables can seem pretty odd, but ultimately it can make things easier. At any rate, unless you have 100% confidence in your bot code, I would take all of that out and write a simple script that does nothing but connect to the db, get a row and print it out. I would also try the simple fetchrow method to get the record as an array. Also, I don't think you need to use MySql. I believe DBI uses the right driver where you have DBI:mysql:.

Code:
use DBI;<br />use MySql;<br />use strict;<br />use warnings;<br /><br />       #connect to db<br />     my $dbh = DBI->connect("DBI:mysql:<database>:<host>","<user>","<password>");<br />       <br />     #retrieve clients info from db<br />     my $query = "select name from users where sn = ?";<br />     my $sth = $dbh->prepare($query);<br />     $sth->execute($client);<br />     <br />     my @user_array = $sth->fetchrow();<br />     print @user_array;<br /><br />     $sth->finish();<br />     $dbh->disconnect();
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Thu Mar 31, 2005 3:24 pm    Post subject: Reply with quote

QUOTE(xiloki @ Mar 31 2005, 12:25 AM)
Thanks.  That did it.  I'm not familar with perl and perl syntax... I've programmed in c and java and i know php, html etc.
I don't understand why I'm having such an issue learning perl.
Anyways I no longer get that error but it returns a blank im.  Any ideas?
[right][snapback]47385[/snapback][/right]


It does not send what you print as a message. You are printing the data out, but then sending $reply to the user. If you want to send them a message then you need to populate relply with something.

Code:
<br />sub on_im{<br />my ($aim,$client,$msg,$away) = @_;<br /><br /><br /> #filter message for html<br /> $msg =~ s/<(.|\n)+?>//g;  <br /><br /> <br />  #send im<br />  my $reply = "test";<br /> #$aim->send_im($client,$reply);<br /> <br /> #print response to im window<br /> print "[$screenname] $reply \n";<br /> <br /> #stop the typing status<br /> $aim->send_typing_status ($client, TYPINGSTATUS_FINISHED);<br /><br /><br />}<br />1;<br />


That would send "test" as the message.

_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
xiloki
Newbie
Newbie


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

PostPosted: Thu Mar 31, 2005 6:57 pm    Post subject: Reply with quote

Sorry I should have stated that I changed the code
Code:
<br />   <br />           #connect to db<br />      my $dbh = DBI->connect("DBI:mysql:<database>:<host>","<user>","<password>");<br />        <br />      #retrieve clients info from db<br />      my $query = "select name from users where sn = ?";<br />      my $sth = $dbh->prepare($query);<br />      $sth->execute($client);<br />      <br />      while ( my $hashref = $sth->fetchrow_hashref()) {    <br />         print $hashref->{"name"};<br />         }<br />      $sth->finish();<br />      $dbh->disconnect();<br />      <br />  #do work<br />  my $reply = "working with mysql....";<br />  <br />     #send im<br />  $aim->send_im($client,$reply);<br />  <br />  #print response to im window<br />  #print "[$screenname] $reply \n";<br />  <br />  #stop the typing status<br />  $aim->send_typing_status ($client, TYPINGSTATUS_FINISHED);<br /><br />   


I just want it to print to the cmd window the info from the db and send an im that says "working with mysql..." for right now. But it doesn't print anything at all
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Thu Mar 31, 2005 7:21 pm    Post subject: Reply with quote

In that case, I would definitely do what I suggested, create a simple script that connects to the db, gets the data and prints it out, minus all the IM code. It will then be much easier to debug. Is it not making the db connection, is it not finding the correct database or table, is there any content in the db, etc.?
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Thu Mar 31, 2005 7:43 pm    Post subject: Reply with quote

I would add more to the print statment. like
Code:
<br />print "TEST:" . $hashref->{"name"} . "\n";<br />


Then you would know that it was looping right. It is possible that your $client variable doesn't match the SN of any rows in your database.

_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
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