Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database Query Values

Status
Not open for further replies.

venkatpavan

Programmer
Feb 18, 2006
42
SG
Hi,

Could someone help me on how to get the values from sql query,Below is the query i'm using to get 3 values and trying to use later but I'm getting only one value from the query,Below is the query which I'm using in my script.


$sql = qq {SELECT FieldA,FieldB,FieldC FROM TABLE Where X=X };
print $sql;
$sth = $dbconnection -> prepare ($sql)
$sth -> execute
$query_results = $sth -> fetchrow();

if ($query_results > 0) {
my @res = $query_results;
if($res[1] == $Value1 and $res[2] == $Value2) {
Value = $res[0]; }
else {
if($res[1] != $Value1){
DO Something;}
else {DO something }


Thanks,
 
When you say you are getting one value from the query, I assume you mean that 2 of the 3 fields in your SELECT are null. Which ones? Did you check what values are actually in your database table for a given record?

Also your WHERE condition looks suspicious. It always evaluates to true (WHERE X=X). I assume that's ok, as you probably supplied a generic query for the purpose of this post.
 
Thanks for the reply!

Yes condition X=X provided as generic.

All the three fields have values and select query is returning all the three but I could not able to read all the three values,$query_result is returning only one value i.e first field.

Thanks,
 
Its been a long time since I actually used the DBI directly. Many years ago I wrote a wrapper routine which simulated the SQL interface I had with Perl 4. (The programmer had called it SQerl - from SQL and Perl) The interface was so much more intuitive, that I could not let go of it when I upgraded to Perl 5.

So looking at my wrapper routine I use fetchrow_arrayref() instead of fetchrow(). Here is the piece of code I pulled out. If memory serves me correctly, each array element represents a field. so element [0] of @data_ptr is the first field, element [1] is the second field of the query.
The while-loop adds each record into an array, @write_to. Each field is delimited by the delimiter, $field_delimiter.

Code:
while ( $data_ptr = $sth->fetchrow_arrayref() ) {
     push(@$write_to, join($field_delimiter, @$data_ptr));
}
I hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top