FinalFrontier Posted December 15, 2011 Share Posted December 15, 2011 I use: $query = "SELECT * FROM orders WHERE customeremail = joe@example.com"; $result = mysql_query($query); $row = mysql_fetch_array($result); mysql_query ($query); I then use: while ($row = mysql_fetch_array($result)) { $random = rand(10000000, 99999999); $query = "UPDATE orders SET laybypassword = ".$random." WHERE id = ".$row['id'].""; mysql_query ($query); } The first of the two rows updates, but the second doesn't. Why is that? Am I suppose to be using something like foreach()? Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted December 15, 2011 Share Posted December 15, 2011 Ok, there's a number of things wrong with this, I'm going to comment on every line: //this query is malformed and will throw a mysql error. You have to quote the email address. $query = "SELECT * FROM orders WHERE customeremail = joe@example.com"; //this runs the query and puts the results in $result $result = mysql_query($query); //this fetches the first row into $row. YOU NEVER USE THIS RESULT $row = mysql_fetch_array($result); //this runs the query a second time for no reason mysql_query ($query); //this loops through the result set starting at row #2, since you already fetched row #1 up there and discarded it. while ($row = mysql_fetch_array($result)) { $random = rand(10000000, 99999999); //If this field is a string password, the input really should still be quoted even though it's a number. $query = "UPDATE orders SET laybypassword = ".$random." WHERE id = ".$row['id'].""; //this line runs the above query. mysql_query ($query); } //All of this could be done in a single query without MySQL at all. -Dan Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.