Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Perl Mysql connection syntax code error

mysql syntax connection mysql_connect

  • Please log in to reply
6 replies to this topic

#1 kevinsabres

kevinsabres

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 22 April 2010 - 06:59 PM

#!/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?
  • 0

#2 kevinsabres

kevinsabres

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 22 April 2010 - 07:15 PM

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
  • 0

#3 phpforfun

phpforfun

    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1056 posts

Posted 23 April 2010 - 10:14 AM

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."
  • 0

#4 phpforfun

phpforfun

    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1056 posts

Posted 23 April 2010 - 02:03 PM

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";
}

  • 0

#5 kevinsabres

kevinsabres

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 24 April 2010 - 07:38 PM

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
  • 0

#6 kevinsabres

kevinsabres

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 24 April 2010 - 08:20 PM

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
  • 0

#7 phpforfun

phpforfun

    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1056 posts

Posted 25 April 2010 - 12:01 PM

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";

  • 0





Also tagged with one or more of these keywords: mysql, syntax, connection, mysql_connect

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download