Jump to content

fetching database records using loops in per probleml

- - - - -

  • Please log in to reply
1 reply to this topic

#1
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
good day everyone, am trying to print a information if the price of televison where okay but it does not print
anything. it seem i did not loop the records very well
any help please. below is the code
#!/xampp/perl/bin

$dbh = "dbi:mysql:testDB:localhost:3306"',"root","root77";

$query = "SELECT price FROM product where television='panasonic'";
$rs = $connect->prepare($query);

$rs->execute();

# BIND TABLE COLUMNS TO VARIABLES
$rs->bind_columns(undef, \$price);


# check if the price is greater than $10,000 AND this where am having prblem

if ($rs[0] > 10000) {
print  "price is greater than 10000";
}


elsif($rs[0] = 10000) {

print "correct";


}

else{
print "you get it wrong";
}



















#2
numberwhun

numberwhun

    Newbie

  • Members
  • Pip
  • 2 posts
First, your code tags are not complete. Your last tag should have a ] after it. Second, you do not have any loops in your code. You have an if statement and are testing the current value of $rs[0], assuming it has a value, but there are no loops present in your code.

I first recommend that you add the following pragmas to your code after the shebang line:

use strict;
use warnings;

Every Perl script you write should have those in it to avoid putting code in here with basic syntactical issues. After that, you will need to re-write the code with a loop if you wish to loop through $rs, but even before you do that, $rs would first need to be defined as @rs. You also don't have any fetch_array uses in the code, so while you executed the statement, your not pulling any data.

You should really go through a Perl DBI tutorial to learn the proper way to use it first, and you may then understand what is going wrong with the code you have posted.

Regards,

Jeff




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users