Current location: Hot Scripts Forums » Programming Languages » Perl » loop through & update a mysql database


loop through & update a mysql database

Reply
  #1 (permalink)  
Old 11-08-03, 12:37 PM
ednit ednit is offline
New Member
 
Join Date: Nov 2003
Location: US
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
loop through & update a mysql database

I am having trouble correctly updating a database. . . here's the code:


$nada = "0";
&OPENdb;
$table = "info";
$checkit = "SELECT * FROM $table WHERE id_clicks > '$nada'";
$results = $dbh->prepare($checkit);
$results->execute();
while ($ref = $results->fetchrow_hashref()) {
$clicks = $ref->{'id_clicks'};
$weekly = $ref->{'weekly_id_clicks'};
$new_week = $clicks + $weekly;
$table = "info";
$change = "UPDATE $table SET id_clicks='$nada', weekly_id_clicks='$new_week' WHERE id_clicks >'$nada'";
$dbh->do($change);}
&CLOSEdb;


If I take the $change part off and print the info, (like the $clicks, weekly, and $new_week )it prints all the data out. . . but when I go to update the database, all the $new_week values are the same. . . Someone please help!
Reply With Quote
  #2 (permalink)  
Old 11-08-03, 01:50 PM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by ednit
I am having trouble correctly updating a database. . . here's the code:


$nada = "0";
&OPENdb;
$table = "info";
$checkit = "SELECT * FROM $table WHERE id_clicks > '$nada'";
$results = $dbh->prepare($checkit);
$results->execute();
while ($ref = $results->fetchrow_hashref()) {
$clicks = $ref->{'id_clicks'};
$weekly = $ref->{'weekly_id_clicks'};
$new_week = $clicks + $weekly;
$table = "info";
$change = "UPDATE $table SET id_clicks='$nada', weekly_id_clicks='$new_week' WHERE id_clicks >'$nada'";
$dbh->do($change);}
&CLOSEdb;

If I take the $change part off and print the info, (like the $clicks, weekly, and $new_week )it prints all the data out. . . but when I go to update the database, all the $new_week values are the same. . . Someone please help!
try it like this, remove the $change line in your code :


$dbh->do("UPDATE $table SET id_clicks='$nada', weekly_id_clicks='$new_week' WHERE id_clicks >$nada");
}

(do not use any quote marks around $nada at the end of the line)

and see if that helps
Reply With Quote
  #3 (permalink)  
Old 11-08-03, 01:58 PM
ednit ednit is offline
New Member
 
Join Date: Nov 2003
Location: US
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
That did not work. . .they still come out the same. I don't get it cuz when I print it out it works, but setting it into the database it only has one value. . . .still clueless
Reply With Quote
  #4 (permalink)  
Old 11-09-03, 09:43 PM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
Something like this should do the trick:

Code:
my $nada = 0;
&OPENdb;

my $table = 'info';
my $sql_1 = qq~SELECT * FROM $table WHERE id_clicks > ?~;
my $results = $dbh->prepare($sql_1);
$results->execute($nada);

# Prepare the SQL only once outside of your loop.  This will
# save you some overehad.  Also, check the DBI docks for placeholders (?), 
# it makes your life much easier when writing SQL statements in perl.
my $sql_2 = qq~UPDATE $table 
               SET id_clicks = ?, weekly_id_clicks = ?
               WHERE id_clicks > ?~;
my $sth = $dbh->prepare($sql_2);

while (my $ref = $results->fetchrow_hashref()) {
  my $clicks = $ref->{'id_clicks'};
  my $weekly = $ref->{'weekly_id_clicks'};
  my $new_week = $clicks + $weekly;
  # Execute your query with the bind values
  $sth->execute($clicks, $weekly, $new_week);
}
&CLOSEdb;
Check into placeholders and bind values. That will make writing your queries a little easier. Here's a link to that section in the DBI pod: http://search.cpan.org/~timb/DBI-1.3...nd_Bind_Values. It takes a bit to get used to them at first but it's worth it. Also, see me comments in the script for a little more detail.

Check out the perl quote operators too: http://www.perldoc.com/perl5.8.0/pod/func/q.html. Not much of an explanation there but q{tetxt} single quotes the text in side the {}. That satement above would be 'text'. The qq{text} is for double quotes: "text". The {} is arbitrary; you can use anything as long as it's not in the string to be quoted: () or [] or !! or $$ and the list goes on. I often ues the ~ in my queries: qq~SELECT foo FROM bar~;. This eliminates the need to escape any quote chars in your queries.

~Charlie
Reply With Quote
  #5 (permalink)  
Old 10-03-09, 10:07 PM
randy1as randy1as is offline
New Member
 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL PHP

Will this work using PHP, reasonably amended ?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
PHP to MySQL script question...(using a field to update info in MySQL) DisneyFan25863 PHP 4 11-02-03 03:31 AM
Send sms via php and mysql database help meeeeee eggdesign PHP 2 10-03-03 12:29 AM
type mismatch and update loop - HELP! seala ASP 1 09-22-03 05:27 PM
What is the best free discussion board with MySQL database? jrobbio General HotScripts Site Discussion 44 06-27-03 03:59 PM


All times are GMT -5. The time now is 04:42 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.