Prolix Posted November 19, 2008 Share Posted November 19, 2008 Hi. I've just started working on a PHP (5.2.1) project and am having a hard time fetching rows as arrays in a while loop from my database (mySQL 4.1.22). The code below works perfectly if I use *if* on the marked line, and handles the first row of the data set as I would expect. But when I change it to *while*, nothing happens, just punctuation marks on the screen and when I check the table with phpMYAdmin, nothing changes (I reset the database manually between tests). Any suggestions? <?php include ('../string_link.php'); # Set error reporting and connect to database $query = 'SELECT ek, prform FROM dchords'; $result = mysql_query ($query) or die (mysql_error()); if ($row = mysql_fetch_assoc ($result)); { # WORKS PERFECTLY WITH "if", BUT NOT "while" $prform=$row['prform']; $ek=$row['ek']; $v_beauty=$prform+1; print "$ek: $prform, $v_beauty. "; $query = "UPDATE dchords SET v_beauty=$v_beauty WHERE ek=$ek LIMIT 1 ;"; mysql_query ($query); } ?> p.s., The fields are all integers, and I only have 7 rows of data in the table for testing. Default value of $v_beauty is NULL. Quote Link to comment https://forums.phpfreaks.com/topic/133400-solved-newbie-stumped/ Share on other sites More sharing options...
darkfreaks Posted November 19, 2008 Share Posted November 19, 2008 haha change mysql_fetch_assoc() to mysql_fetch_array() and it should work with the while <?php include ('../string_link.php'); # Set error reporting and connect to database $query = 'SELECT ek, prform FROM dchords'; $result = mysql_query ($query) or die (mysql_error()); while($row = mysql_fetch_array($result)) { $prform=$row['prform']; $ek=$row['ek']; $v_beauty=$prform+1; print "$ek: $prform, $v_beauty. "; $query = "UPDATE dchords SET v_beauty=$v_beauty WHERE ek=$ek LIMIT 1 ;"; mysql_query ($query); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/133400-solved-newbie-stumped/#findComment-693818 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 <?php include ('../string_link.php'); # Set error reporting and connect to database $query = 'SELECT ek, prform FROM dchords'; $result = mysql_query ($query) or die (mysql_error()); while($row = mysql_fetch_assoc ($result)) { # WORKS PERFECTLY WITH "if", BUT NOT "while" $prform=$row['prform']; $ek=$row['ek']; $v_beauty=$prform+1; print "$ek: $prform, $v_beauty. "; $query = "UPDATE dchords SET v_beauty='$v_beauty' WHERE ek='$ek' LIMIT 1 ;"; mysql_query ($query); } ?> In your update statement you need the single quotes around the data so it does not throw errors, especially if it is in a string format. Also you have a semicolon after the if statement. Above is an updated code and should work. As per the remark above, mysql_fetch_assoc will work fine, and if you are not planning on also using the index in the array is better because it does not return both assoc array and and index array which will double the amount of data coming into the script. Quote Link to comment https://forums.phpfreaks.com/topic/133400-solved-newbie-stumped/#findComment-693828 Share on other sites More sharing options...
darkfreaks Posted November 19, 2008 Share Posted November 19, 2008 what premiso said ^ beat me to the extra semi before the while and the query error Quote Link to comment https://forums.phpfreaks.com/topic/133400-solved-newbie-stumped/#findComment-693830 Share on other sites More sharing options...
Prolix Posted November 19, 2008 Author Share Posted November 19, 2008 48 hours of staring at the screen and it was a silly semicolon. /laughing and crying Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/133400-solved-newbie-stumped/#findComment-693836 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.