Okay, I have gotten MySQL and Perl working together for a CGI script. Everything is working fine, but I can't help but stare at the lengths I have to go through when I want to get a single value from the database. Below are two samples of code that work, but I am looking to see if a shorter way exists:
# Example 1: fetchrow_arrayref
my $sth = $db->prepare("SELECT the_name FROM the_table WHERE id=3");
$sth->execute();
print $sth->fetchrow_arrayref()->[0];
$sth->finish();
# Example 2: fetchrow_hashref
$sth = $db->prepare("SELECT the_name FROM the_table WHERE id=3");
$sth->execute();
print $sth->fetchrow_hashref()->{'the_name'};
$sth->finish();
I skipped the while{} loop for the fetchrow_hashref() because this is an instance where I know for a fact that there will only be one value returned. My real question is whether or not the value I am retrieving can be put in a variable during the call to prepare() (perhaps replacing it with do()). Something like this perhaps:
my $sth = $db->do("SELECT the_name FROM the_table WHERE id=3");
print $sth;
Thanks ahead of time (assuming you help me) :)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|