[thelist] is this correct PHP/SQL or a better way?

Kelly Hallman khallman at wrack.org
Tue Jan 7 15:34:00 CST 2003


On Tue, 7 Jan 2003, Tom Dell'Aringa wrote:
> Here is the code:

Modified your while loop:
-----------------------------------------
$uid  = $_GET["uid"];
$sqlquery = "SELECT pid, qty FROM $name";
$qres = mysql_query($sqlquery);
while($row = mysql_fetch_array($qres))) {
    $pid = $row["pid"];
    $qty = $row["qty"];

    $sql = "UPDATE registry SET bought = bought + $qty
        WHERE prodtempID = '$pid' AND reguserID = '$uid'";
}

> ------------------------------------------
> Question 1) Is there a better way with some kind of FOR loop?

I modified your code above..  I don't think
!mysql_numrows(@mysql_query($q)) is a good while condition because it's
going to continuously evalute to true, as long as you have rows in the
result.  Better to fetch the array as your while condition.  When there
are no more result rows, it will return false ending the while loop..

> Question 2) In my SQL, I need what I *think* is called an aggregate
> value(?). I need to take the $qty value and ADD it to the bought
> value that already exists for that row. I don't think my SQL is
> correct there though...

I'll defer to the SQL gurus on that one, but I think you can use that
UPDATE statement as is.  I believe an aggregate value is one that combines
values from more than one row, though...to be semantic.

--
Kelly Hallman
http://wrack.org/




More information about the thelist mailing list