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

sasha spam at bittersweet2.com
Tue Jan 7 16:05:01 CST 2003


On Tue, 7 Jan 2003 13:22:55 -0800 (PST), Tom Dell'Aringa
<pixelmech at yahoo.com> wrote:

> Here is the code:
> -----------------------------------------
> $uid  = $_GET["uid"];
> $result = "SELECT pid, qty FROM $name";
> while(!mysql_numrows(@mysql_query($result)))
> {
> $row = mysql_fetch_array($result);
> $pid = $row["pid"];
> $qty = $row["qty"];
>
> $sql = "UPDATE registry SET
> bought = bought + $qty
> WHERE
> prodtempID = '$pid' AND reguserID = '$uid'";
>
> //exectute $sql,etc.
> }
> ------------------------------------------
> Question 1) Is this a good way with PHP to loop through my result set
> (I need to hit every row) or there a better way with some kind of FOR
> loop?

This is how I do mine:

if (!@$result = mysql_query("select pid, qty from whatever")) {
	something isn't right;
	exit();
}

if (mysql_num_rows($result)) {
	while (list(pid, qty) = mysql_fetch_row($result)) {
		do stuff with result
		(fyi, fetch row is a little faster than fetch array, but you don't get
array key names)
	}
}
else {
	there was nothing to select
}

You could try an update query like this:
"update registry set bought = '" . $bought + $qty . "' where prodtemp
etc...."

--
sasha



More information about the thelist mailing list