0

So basically what I'm trying to do is for each row that has been generated by the query, I try to generate a URL and visit that URL. The funny part is that it always only visits the FIRST generated URL and never visits the others.

What am I doing wrong? Please help me.

$query = "SELECT distinct b.productname, b.seller, b.price, b.offerid 
          from tracker b";
$results = mysqli_query($dbcon, $query);

$rows = array();
$i = 0;

while ($row = mysqli_fetch_assoc($results)) {
    $rows[] = $row;

    foreach ($rows as $row) { 
        $url = 'url'.$i;
        $$url = 'https://bla.com/tools/tracker.php?productID=' .
                $row["productname"] . '&verkoper=' . 
                $row["seller"] . '&offerid=' . 
                $row["offerid"] . '&price=' . $row["price"] . 
                '&productTracken=';

        // set URL and other appropriate options
        file_get_contents($$url);

        $i++;
    }
}
1
  • Good code indentation would help us read the code and more importantly it will help you debug your code Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. Commented Nov 17, 2021 at 16:11

2 Answers 2

3

If you simplify what you are doing you may get the result you want

All you seem to be doing is touching a list of urls built from data in a table

Also you dont need a while loop and a foreach loop, that is what is definitely causing your problems.

$query = "SELECT distinct b.productname, b.seller, b.price, b.offerid 
          from tracker b";
$results = $dbcon->query($query);

while ($row = $results->fetch_assoc()) {

    $url = 'https://bla.com/tools/tracker.php?productID=' .
                $row["productname"] . '&verkoper=' . 
                $row["seller"] . '&offerid=' . 
                $row["offerid"] . '&price=' . $row["price"] . 
                '&productTracken=';

    // set URL and other appropriate options
    file_get_contents($url);
}
Sign up to request clarification or add additional context in comments.

11 Comments

file_get_contents() can use URL if the fopen wrappers have been enabled. Maybe use cURL as alternative is recommended?
I got this from the logs site usage logs. It only visits the first URL in the array for some reason.. [17/Nov/2021:17:33:02 +0100] "GET /tools/tracker.php?productID=112&verkoper=b12&offerid=123&price=6&productTracken= HTTP/1.0" 302 30202 "-" "-"
Using this code? Or your original code. Small Point You dont have an array, I dont have an array, this code gets one row at a time from a resultset and makes it into a url
Using your code. 302 doesnt really matter as I see that the URL has been visited, just not the other URL's..
|
1

You have a couple of odd things happening here.

First, your loops:

while ($row = mysqli_fetch_assoc($results)) {
    $rows[] = $row;

    foreach ($rows as $row) { 

Each time you fetch a row from the database, you add it to an array. You then immediately loop over all the items in that array. If you output a row number each time, you'd get 0; then 0, 1; then 0, 1, 2; and so on.

Secondly, your URL variable:

$url = 'url'.$i;
$$url = 'https://...';

This uses an esoteric feature of PHP called variable variables: you choose dynamically what variable name you want. This feature is basically never needed, because it's almost always better to use an array:

$urls[$i] = 'https://...';

In this case, though, you're only using one value at a time, so you can just use a plain old variable:

$url = 'https://...';

Fixing these gives you the code in RiggsFolly's answer, and will probably fix your problem.

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.