1

I have to do a first query which is SELECT * from table WHERE NOT column=0, and use the results from that to do a foreach loop something like below.

foreach($results as $result) { 
$nox = $result[field_x]; 
//Use nox for whatever; 
//Then update some fields in SQL; }

And keep doing that until all the items in the first query are done.

What would the syntax be?

1
  • jesus christ dont tempt me, it would be just too easy to close the lid on the laptop right now Commented Aug 16, 2009 at 23:21

3 Answers 3

3

Unless you have some extensive processing done in PHP you can most likely do that in a single, multi-table UPDATE.

For example:

UPDATE table1 t1
  JOIN table2 t2 ON t2.id = t1.id
   SET t1.col = t2.col + 1
 WHERE t1.somecol <> 0
Sign up to request clarification or add additional context in comments.

1 Comment

it really is extensive php processing! thats why i wanted to make sure my way of thinking was alright. thanks anyway
1

.

$sql = "SELECT * FROM `myTable` WHERE `column` = 0";

// query the database
$resource = mysql_query($sql);

// loop through the results
while ($result = mysql_fetch_assoc($resource)) {
    // grab the value
    $nox = $result['field_x'];   // you should use quotes here, btw

    // make your modifications;
    ++$nox; // or whatever

    // build the query
    $sql = sprintf(
        "UPDATE `myTable` SET `field_x` = %d WHERE `id` = %d"
        , $nox
        , $result['id']
    );

    // run the query
    mysql_query($sql);
}

2 Comments

ok thanks, Im really not being lazy or stupid, im just lazy, stupid and tired after a long day!
basic I know but the start would be: $query = "SELECT * FROM table WHERE NOT field_x=0"; $results = mysql_query($query); right?
0

You'd just use a simple update command:

update tbl set field = '$value' where id = $id

In PHP-ese:

$sql = 'update tbl set field = \'' .
        mysql_real_escape_string($value) . '\' where id = ' . $id;
mysql_query($sql);

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.