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