Jump to content

Perl Mysql connection syntax code error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
kevinsabres

kevinsabres

    Newbie

  • Members
  • Pip
  • 9 posts
#!/usr/bin/perl 


useDBI;


chomp($id = <STDIN>);

$database="";

$user="";

$host="localhost";

$password="";


$dbh=DBI->connect('DBI: mysql: $database: $host, $user,$password') or die "Can't connect: ", DBI->errstr;


$sth = $dbh->prepare("SELECT id, title, author from book where id = $id");

$sth->execute();


print "Contents of book table."


while(@row = $sth->fetchrow_array()) 

{

	print "id:$row[0]\n";

	print "title:$row[1]\n";

	print "author:$row[2]\n";

}


$sth->finish();

$dbh->disconnect;

i keep getting compile erros when i run this in a unix shell. i took out the values of the variables for the passwords to post this here...

i get a syntax error it says near ) { on the while look and at near the } on the while loop.

anyone know what is wrong wit hthis code?

#2
kevinsabres

kevinsabres

    Newbie

  • Members
  • Pip
  • 9 posts
I've never connected to a mysql database btw, just used a few tutorials online and from a book but cant get it to work

#3
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Just a small observation, I do believe there should be a space between use and DBI... And also, you dont have a ; at the end of your
print "Contents of book table."
Checkout my new forum! http://adminreference.com/

#4
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Here is one I set up a while ago....

#!c:/perl/bin
use DBI;
use DBD::mysql;

# MySQL CONFIG VARIABLES 
%db =     ("host",         "mysqlc2",
         "database",     "tstaudit",
         "tablename",     "audits",
         "user",         "xxxxxxxxx",
         "pw",             "xxxxxxxxxxx");
         
$dsn        = "dbi:mysql:$db{'database'}:$db{'host'}:3306";
$connect     = DBI->connect($dsn, $db{"user"}, $db{"pw"}) or die "Unable to connect: $DBI::errstr\n";

# Set and prepare query 
$query         = "SELECT * FROM `audits`";
$query_handle = $connect->prepare($query);

# Execute 
$query_handle->execute();

# Bind table and column values 
$query_handle->bind_columns(undef, \$id, \$ticket, \$agent_id, \$test);

# Loop through the results 
print "<pre>\n";
print "ID\t Ticket\t\t\t Agent\t Test\n";
print "=============================================\n";
while($query_handle->fetch()) {
   print "$id\t $ticket\t $agent_id\t $test\n";
} 

Checkout my new forum! http://adminreference.com/

#5
kevinsabres

kevinsabres

    Newbie

  • Members
  • Pip
  • 9 posts
thanks for the help, this is my first time trying this for a school project

thanks for the help those 2 things you mentioned got it to say the syntax is ok.... BUT now when i run it i get this error do you knwo what it means:

Can't connect(DBI: mysql: $database: $host, $user, $password), no database driver specified and DBI_DSN env var not set at pbad.perl line 12

#6
kevinsabres

kevinsabres

    Newbie

  • Members
  • Pip
  • 9 posts
ok I think i fixed it by altering it to:

$dbh=DBI->connect("DBI:mysql:databaase=$database:host=$host", $user, $password) or die "Can't connect: ", DBI->errstr;

BUT now i get these 2 errors:

DBD::mysql::st execute failed: No database selected at pbad.perl line 15, <STDIN> line 1.
DBD::mysql::st fetchrow_array failed: fetch() without execute() at pbad.perl line 19, <STDIN> line 1.

any help is greatly appreciated

#7
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Make sure to use..

use DBI;
use DBD::mysql;
And..


$dsn        = "dbi:mysql:$database:$host:3306";
$dbh     = DBI->connect($dsn, $user, $password) or die "Unable to connect: $DBI::errstr\n";

Checkout my new forum! http://adminreference.com/