LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2000, 06:26 PM   #1
bickford
Member
 
Registered: Jun 2000
Location: SUNY Buffalo
Posts: 79

Rep: Reputation: 15
Unhappy


Ok, here's a code snippet from my perl program phonebook.pl:
sub show_entries {
my ($dbh, $sth, @row);
$dbh = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort",$serverUser,$serverPass) ;
$sth = $dbh->prepare("SELECT Name FROM $serverTable");
$sth->execute;

When I http://localhost/cgi-bin/phonebook.pl I get the following error:

Software error:
Can't call method "prepare" on an undefined value at /home/httpd/cgi-bin/phonebook.pl line 22.

Line 22 is the line that begins "$sth=$dbh->prepare...".

I've declared:
my $serverName= "localhost";
my $serverPort = "3306";
my $serverUser = "listing";
my $serverPass = "password";
my $serverDb = "phonebook";
my $serverTable = "listing";

Within the "phonebook" database I have a table correctly setup called "listing" with a column called "Name" with some values in it.

I've also done:
insert into host(Host,Db)values('localhost','phonebook');
insert into user(Host,User,Password) values ('localhost' ,'listing','PASSWORD('password'));

Any idea what's going on?
 
Old 08-06-2000, 07:22 PM   #2
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,597

Rep: Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080
Try it with the $dbh line as
Code:
$dbh = DBI->connect("DBI:mysql:databasename",$serverUser,$serverPass);

 
Old 08-06-2000, 07:43 PM   #3
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,597

Rep: Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080
As I look at that code I realize it may not work. I do not code in the exact style you have there, so I am going to show you what I know works;

Code:
use Mysql;

$dbh = Mysql->Connect("localhost","dbname","username","password");

$sql = "SELECT Name FROM $serverTable";
$res = $dbh->Query($sql);
Then you can loop through the results with
Code:
while (@tempary = $res->fetchrow){
     $first_column = $tempary[0];
     #etc, etc, etc.
}
Let me know if that does what you want.
 
Old 08-07-2000, 12:35 AM   #4
bickford
Member
 
Registered: Jun 2000
Location: SUNY Buffalo
Posts: 79

Original Poster
Rep: Reputation: 15
still havin issues

Ok, well I migrated my code to your solution and basically I'm still having the same problem, oddly enough:

Can't call method "Query" on an undefined value at /home/httpd/cgi-bin/phonebook.pl line 24.


Line 23 and 24:
$sql = "SELECT Name FROM $serverTable";
$res = $dbh->Query($sql);

Seeing as how we've gotten the same error doing it two different ways the problem must lay elsewhere. What do I have to do in order to allow connectivity to my database? Is everything I said I did in my previous post enough? How can I check if the user "listing" has access to the database? One curious thing I found when I "telnet localhost 3306" I get the following error:

Host 'localhost.localdomain' is not allowed to connect to this MySQL serverConnection closed by foreign host.

Is that a problem?

Thanks for the help. =)
 
Old 08-07-2000, 12:39 AM   #5
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,597

Rep: Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080
It may be an access problem. If you type (at a command prompt) mysql -u username -p password databasename (substituting where applicable )what happens?
 
Old 08-08-2000, 07:44 PM   #6
kill-hup
Member
 
Registered: Aug 2000
Location: NY - USA
Distribution: Slackware
Posts: 109

Rep: Reputation: 15
Lightbulb

Definitely looks like an access problem. If you receive the "access denied" message, you need to edit the access permissions within mysql to allow yourself to connect to the db. To do this, you have to check the "user" table in the database "mysql". After making changes to the user table, issue the command (from shell prompt) "mysqladmin flush-privileges".

You really should check through the access anyway, just to be sure who has access to your db =)
 
Old 08-08-2000, 11:18 PM   #7
bickford
Member
 
Registered: Jun 2000
Location: SUNY Buffalo
Posts: 79

Original Poster
Rep: Reputation: 15
pretty much working

Thanks, yea it definitely was an access/priveleges problem. I was getting rather frusterated as I had even the user table just as I thought it should be and I still couldn't connect, but as per your post I hadn't flushed the privileges. After I did that it worked like a charm. I'm loving SQL, nuts to that dung from Redmond.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with MySQL on SuSE: Can't Connect (/var/lib/mysql/mysql.sock) neocookie Linux - Software 8 02-07-2008 11:48 PM
Bugzilla - MySQL - perl - DBD::mysql install problem Runningonair Linux - Software 8 10-12-2007 12:42 AM
Error compiling perl-DBD-MySQL after upgrading to MySQL 4.1 pitaro920 Linux - Software 1 01-16-2006 04:28 PM
Apache Perl Mysql installation problems ridge321 Linux - Newbie 1 02-15-2005 08:49 AM
MySQL and Perl c0c0deuz Linux - Software 4 12-19-2001 11:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration