NeverSayDie wrote: You'd be best to brush up on your SQL, there's loads of tutorials on the web. SELECT statements are used to retrieve fields from records that match values that you specify. Eg, "SELECT firstname, secondname FROM customer WHERE customer.income = 50", or "SELECT name FROM pet WHERE age > 5 AND numChildren = 2", that kind of thing. Google for tutorials, or check the MySQL manual - there's loads of info in there. Re getting results back to Perl, you need to take another read of that DBI tutorial I linked to, particularly the sample code. Once you've prepared a statement, you need to execute it, and in the case of a SELECT, call fetchrow_array() or an equivalent method if you want to get back the results.
NeverSayDie wrote: The problem here is that you're throwing raw SQL instructions into the middle of your Perl code, which isn't going to work. Basically, you need to use DBI methods to send your SQL statements to the database, and retrieve the results. It's pretty straightfoward, see here for a tutorial:http://www.perl.com/pub/a/1999/10/DBI.html I'm assuming you're comfortable with Perl btw, if you're not, this book is a good bet, nice balance of introduction and reference:http://www.bookpool.com/sm/0596000278
#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use DBI; my $database = "d_name"; my $data_source = "DBI:mysql:$database:localhost"; my $username = "user1"; my $password = "pass1"; my $dbh = DBI->connect( $data_source, $username, $password) ; my $sth = $dbh->prepare('create table names (myname CHAR(10), yourname VARCHAR(10))'); my $sth = $dbh->prepare('CREATE table names (myname CHAR(10), yourname VARCHAR(10))'); my $sth = $dbh->prepare('INSERT INTO names VALUES (Monty,Monty2)'); my $sth = $dbh->prepare('SELECT myname = Monty, yourname = Monty2 FROM names;'); $dbh->disconnect; print "Content-type:text/html\n\n"; print "Hello World\n\n$sth\n";
#!/usr/bin/perl use DBI; use CGI::Carp qw(fatalsToBrowser); my $database = "database_name"; my $data_source = "DBI:mysql:$database:localhost"; my $username = "database_name"; my $password = "pass1"; my $dbh = DBI->connect( $data_source, $username, $password) ; USE database_name; CREATE TABLE names (myname CHAR(10), yourname VARCHAR(10)); INSERT INTO names VALUES ('Monty ', 'Monty '); SELECT myname = 'Monty ', yourname = 'Monty ' FROM names; $dbh->disconnect; print "Content-type:text/html\n\n"; print "Hello World\n";
Zaltais wrote: Assuming you have MySQL already installed then you're ready to go (DBD::mysql just allows Perl to connect to MySQL - you still need MySQL installed for it to work). As for tutorials, I can't really make any recommendations - it's been too long since i learned, and I can't find any of those old tutorials any more. But if you do find a tutorial you think is good enough to follow (you should try to find one that covers creating the database tables and querying the database) and are having any problems, you know where to ask...
finnpark wrote: I was given 3 options this time, I installed option 1 which was DBD::MYSQL. Didn'y install the other 2...hope this is ok? So am I now ready to start googling for tutorials? Is there anything else I need to download? Again, as usual, your info has been extremely helpful and much appreciated...nearly there now.:)
Garfieldus wrote: finnpark, you owe it to yourself to check out CodeCharge Studio. Download a 20 day fully working version and never look back, seriously. PHP, ASP, JSP, Perl, it does it all. MSQL, MySQL, Access, PostGre, it does it all.http://www.codecharge.com
install DBD::mysql
Zaltais wrote: I don't think DBI is included in ActiveState perl, however, installation is trivial from a command-prompt run ppm this will launch the activestate package manager. then type install DBI and ppm should do it's magic...
ppm
install DBI
ishnid wrote: Unless you're intending on treating this as a learning exercise for getting to know PHP, I'm not sure why you wouldn't use Perl if you have skills in it already. The DBI module (which comes as standard with any Perl installation) will take care of the database interaction. You appear to be picking PHP for the wrong reasons (i.e. it's popular), rather than because you want to learn it. There are plenty of Perl resources around the web to help you out too, including forums (this being my most regular haunt, for example).
finnpark wrote: That's brilliant. Great stuff thanks. Can anyone give me a link to a site that allows me to download php, mysql and apache all in 1 go. Ive heard that some of these are reccommended and some are not, any links to this would be great. I want to install apache on my local PC along with MYSQL/PHP in order to test my scripts locally. Thanks again.
sean_or99 wrote: You should be able to put something together very fast. Some advice on using PHP and mysql is:Leverage existing code in PEAR Use a db abstraction layer, like adodb The php manual is one of the best php resources Understand the relational db model before doing anything in mysql Normalize your data Pick a coding standard and stick with it Phpmyadmin is pretty much essential Using a templating system can be very advantageous, like smarty, to start off with. To become very proficient in the LAMP setup I would say takes about 2 years.
finnpark wrote: Cheers Jayo:) . How many hours of work would be required to reach a high standard of the above 1 and 2? In other words how long should it take me to be able to put a complex database together so that large amounts of data can be queried in many different ways to compare methods with stored results. I want to become expertish in databasing. Many thanks.
cython wrote: There are slight differences between the different SQL databases in some of the commands, but they're broadly the same. Learn one and the others follow suit closely enough that for many applications you'll hardly notice the difference. When it comes to querying the database in given languages, however, you may need to learn different libraries, as I think the functions used to connect are different. If you use PHP, you need to know: -HTML (obviously!) -basic PHP -some database library(ies) -some kind of SQL language. And that's pretty much it
jayo99 wrote: 1) Learn php 2) Learn mysql 3) Some javascript would be handy.. Other than that you're good to go.
Sico wrote: Perl would be fine. Then again, you could learn PHP in a day. Hell, you could even write a CGI in C to do it if you wanted. But under your particular circumstances, Perl would probably be easiest. I'd recommend learning SQL and using either MySQL or PostGRE. SQL is easy to learn, and while it might be overkill for this application, it's handy to know. Then you can use APIs in any popular language to interface with the database. Someone who has more experience using Perl and PHP with SQL might be able to give tips on which would be more suited.