Hey Guys,
I would like to think I am slowly making progress with PHP but even the most simple scripts require endless tweaking to get working! Figured this is the best place to get some advice, and hopefully it will not be long before I can also contribute in a constructive way.
Without a doubt this issue is dead simple so please excuse the ignorance. Also not sure what I should search for to find a solution, which must already exist on the forum.

Here is my code without any repetition and it works fine. The id is retrieved from the database and is successfully passed to the query, which I have verified updates the categoryID for the correct row.

$domain = "domain.com";

print $domain . "<br>";

$sql = mysql_query("SELECT id FROM domains WHERE domain = '$domain'");
$row = mysql_fetch_array($sql, MYSQL_ASSOC);
$id = $row['id'];
mysql_query("UPDATE domains SET categoryID = '22' WHERE id ='$id'");

However, I am really trying to change the categoryID for a large number of records (domains) which are stored in a text file. 1 per line.

This is what I have so far:

// executes the code between { } once per line, until the end of the file is reached.
while (!feof($file)) {
$domain = fgets($file);
$sql = mysql_query("SELECT id FROM domains WHERE domain = '$domain'");
$row = mysql_fetch_array($sql, MYSQL_ASSOC);
$id = $row['id'];
print "UPDATE domains SET categoryID = '22' WHERE id = $id<br>";
}

I would like to see the mysql queries before anything else, thus I am printing them. The while loop executes once for every domain in the text file as expected. However the $id is not printed. I don't understand how it works in the single case but not when the code is repeated. I have used $array['label'] in loops before and it worked fine. 🙁

If anybody could shed some light on my very newb problem I would be very grateful.

Thank you in advance,

Hal

:eek:

    Try changing this line...

    $domain = fgets($file);

    ...to this...

    $domain = trim(fgets($file));

    ...mainly to get rid of the newline characters.

    However, if you want to be more efficient about it and reduce everything down to one query, you could do something like (untested):

    <?php
    $domains = file($file);
    array_walk($domains, create_function('&$v, $k', 'trim($v);')); // trim newlines
    $domains = array_filter($domains); // make sure no empty elements
    $values = "'" . implode("','", $domains) . "'";
    $sql = "UPDATE `domains` SET `categoryID` = '22' WHERE `domain` IN ($values)";
    $result = mysql_query($sql) or die("Query failed ($sql): " . mysql_error());
    printf("<p>%s rows were updated</p>\n", mysql_affected_rows());
    ?>
    

      Thanks for your reply NogDog.
      Still not sure what the original problem was but I followed some of your suggestions to simplify my queries and php code and just got the script to execute successfully. =)
      It would have probably been alot quicker for me to complete this task manually but this way is alot more satifying. Btw I didn't know about the array_walk function, looks very useful.

      Lastly,
      create_function('&$v, $k', 'trim($v);')
      Would be great if somebody could explain what the & in &$v does and what is the $k for?

      Thanks,

      Hal

        The array_walk() function sends two parameters to the callback function: the current element's value and the current element's array key. So $v is the value and $k is the key. (They are arbitrary variable names, I could have called them $foo and $bar if I wanted.)

        The '&' before the $v indicates to pass that parameter (the array element's value) by reference, instead of by copy, which is the default method. This allows the function to directly modify the value in the array instead of only modifying a copy of the value local only to the function itself.

          Write a Reply...