Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Q & A
by eg (Friar) on Dec 14, 2000 at 23:50 UTC | |
| [reply] | |
|
Re: Q & A
by bnanaboy (Beadle) on Dec 15, 2000 at 03:59 UTC | |
(leaving out the HTML for the background, etc., and assuming $dbi is the connection to your database, which has a table named QA consisting of 3 columns, Questions, Answers, and ID) 1st case: 2nd case: The script calls itself, passing the ID of the question selected. This way both output pages will have the same format, regardless of future changes made, without having to update multiple files. Using fetch() also retrieves the information one line at a time, so your query won't give you a 10gig hash (as Ovid pointed out). ID is a primary key auto-increment column used to identify each entry in the database. If you don't use that, you could also index Questions and do a select statement using something like WHERE Question like "[first 15 letters]%";, but this is the easiest way I've found. I'm sure there's a better way to do it, though, and I'm always open for suggestions/instruction | [reply] [d/l] [select] |
by repson (Chaplain) on Dec 15, 2000 at 10:46 UTC | |
This technique is know as using placeholders and has several advantages to your technique of interpolation in the SQL statement.
| [reply] [d/l] |
|
Re: Q & A
by coreolyn (Parson) on Dec 14, 2000 at 23:51 UTC | |
I say you should just use one call to the db and pull them into a hash, but wait five to ten minutes and probability says someone here will show me I'm wrong Learning is the process of withstanding ego pain :) coreolyn | [reply] |
by Ovid (Cardinal) on Dec 15, 2000 at 00:00 UTC | |
Hope that wasn't too painful :)
Cheers, Join the Perlmonks Setiathome Group or just click on the the link and check out our stats. | [reply] |
|
Re: Q & A
by Anonymous Monk on Dec 15, 2000 at 00:13 UTC | |
| [reply] |
by $code or die (Deacon) on Dec 15, 2000 at 00:48 UTC | |
You can cycle through each question like this: This is a simple hash, and the keys are the unique identifiers for each question in the database. So when someone clicks on the link, your script would capture the id number and then pull out the answer and display it. Sample output:
| [reply] [d/l] [select] |
by boo_radley (Parson) on Dec 15, 2000 at 00:40 UTC | |
This snippet assumes you've already retrieved the Q&As, and have the Qs in row 0, and the link to the A in row 1. See also DBI's pod. | [reply] [d/l] |