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 />
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 />}
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?
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();
Joined: 03 May 2006 Posts: 2292 Location: Colorado
Posted: Thu Mar 31, 2005 3:24 pm Post subject:
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.
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
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.?
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