1

I have that simple php code:

$sql= 'SELECT ID, fb_postid, scheduled FROM `posts` WHERE clicks = "" AND fb_postid !="" AND scheduled < NOW() order by ID ASC LIMIT 10;';

$result = mysqli_query($link, $sql);
print_r($result);

while ($row = mysqli_fetch_assoc($result)) {
            $clicks=fbcall($fbtoken, $row['fb_postid']);
            $update="UPDATE `posts` SET `clicks`='".$clicks."' WHERE id='".$row['ID']."'";
            $result = mysqli_query($link, $update);     
            print("POSTID: " . $row['fb_postid'] . " - Clicks: " . $clicks ."<br>");    
 }; 

The MYSQL SELECT gets 10 Lines from DB loops through the line in the While Loop, gets "clicks" from function fbcall and then should update all 10 lines with the values from "clicks" to the db. if i run the code without update i get 10 results printed but if i run with mysqli_update i just get 1 row updated. Anybody any Idea why?

5
  • Your code is vulnerable to SQL injection attacks. You should use mysqli or PDO prepared statements with bound parameters as described in this post. Commented Apr 17, 2017 at 19:48
  • what does mysqli_error($link) show on the query? If you want truthness, use mysqli_affected_rows() on UPDATE Commented Apr 17, 2017 at 19:49
  • typo is fixed. Thx. but makes no difference. Commented Apr 17, 2017 at 19:53
  • 1
    I see the problem now.. posted a new answer... Commented Apr 17, 2017 at 19:56
  • no error. and affected rows() is 1. should be 10, Commented Apr 17, 2017 at 19:56

2 Answers 2

1

You're using the $result variable to iterate through a list of rows from the first query.

But you're giving $result a new value within the loop, clearing whatever was there.

Just use two different variables, and you'll be fine. Try something like this:

$sql= 'SELECT ID, fb_postid, scheduled FROM `posts` WHERE clicks = "" AND fb_postid !="" AND scheduled < NOW() order by ID ASC LIMIT 10;';

$result = mysqli_query($link, $sql);
print_r($result);

while ($row = mysqli_fetch_assoc($result)) {
            $clicks=fbcall($fbtoken, $row['fb_postid']);
            $update="UPDATE `posts` SET `clicks`='".$clicks."' WHERE id='".$row['ID']."'";
            $result2 = mysqli_query($link, $update);     
            print("POSTID: " . $row['fb_postid'] . " - Clicks: " . $clicks ."<br>");    
 }; 
Sign up to request clarification or add additional context in comments.

Comments

0

Fixed it and built with prepared update statement:

$sql= 'SELECT ID, fb_postid, scheduled FROM `posts` WHERE clicks = "" AND fb_postid !="" AND scheduled < NOW() order by ID ASC LIMIT 10;';
$result = mysqli_query($link, $sql);
$qry = "UPDATE `posts` SET `clicks`=? WHERE id=?";
$stmt   = mysqli_prepare ($link, $qry);
mysqli_stmt_bind_param ($stmt, 'ss',$clicks, $id);

while ($row = mysqli_fetch_assoc($result)) {
            $clicks=fbcall($fbtoken, $row['fb_postid']);
            $id=$row['ID'];
            mysqli_stmt_execute($stmt);
            print("POSTID: " . $row['fb_postid'] . " - Clicks: " . $clicks ."<br>");    
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.