SundayDriver Posted January 25, 2009 Share Posted January 25, 2009 My first post here and I am asking for help. :-\ But this bug is killing me.... I have an array with bunch of text values in it ($songsalt). I know the contents of the array are intact because I can see them using the "print_r" function. When I run the below code, I see the "songID" for the first value in the array (position "0"), but that's it. No matter what I do, the foreach loop doesn't seem to want to look through the rest of the values in the array. Even if I get rid of the foreach loop, and pull anything out of the array manually, it still doesn't output the songID for anything but the array value at position "0". I am really baffled by this. ??? Any help really would be appreciated. Thanks for reading. (Note: The handle error code does not output an error.) foreach ($songsalt AS $value) { $result = mysql_query("SELECT songtitle FROM songs WHERE songtitle='$value'") or die(mysql_error()); if (!$result) { // Handle error echo 'Query failed. SQL: ', $sql, '<br />Error # ', mysql_errno(), ' Error msg: ', mysql_error(); exit;} while ($row = mysql_fetch_array($result)) { echo $row['songID']; echo "<br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/ Share on other sites More sharing options...
redarrow Posted January 25, 2009 Share Posted January 25, 2009 try this please. <?php foreach ($songsalt AS $value) { $result = mysql_query("SELECT songtitle FROM songs WHERE songtitle='$value'") or die(mysql_error()); } if (!$result) { // Handle error echo 'Query failed. SQL: ', $sql, '<br />Error # ', mysql_errno(), ' Error msg: ', mysql_error(); exit;} while ($row = mysql_fetch_array($result)) { echo $row['songID']; echo "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-745660 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 That if statement won't work because if the query fails the or die will kick in and end the script. Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-745661 Share on other sites More sharing options...
redarrow Posted January 25, 2009 Share Posted January 25, 2009 yes missed that watching tv sorry blade mate. but i am sure you need the loop to stop after the first select statement. Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-745662 Share on other sites More sharing options...
SundayDriver Posted January 25, 2009 Author Share Posted January 25, 2009 Thank you both for the replies. Unfortunately, when I tried redarrow's code, it didn't work. There are still no errors though either. Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-745663 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 add echo mysql_num_rows($result); under the query and tell us what it says Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-745664 Share on other sites More sharing options...
redarrow Posted January 25, 2009 Share Posted January 25, 2009 try this you havent got the song id selected in the select statement. might be wrong theo. <?php foreach ($songsalt AS $value) { $result = mysql_query("SELECT songtitle,songID FROM songs WHERE songtitle='$value'") or die(mysql_error()); } while ($row = mysql_fetch_array($result)) { echo $row['songID']; echo "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-745665 Share on other sites More sharing options...
SundayDriver Posted January 25, 2009 Author Share Posted January 25, 2009 add echo mysql_num_rows($result); under the query and tell us what it says When I ran that, "00001" appears. I guess that is the problem then, huh? The query is only returning one row? But how could that be...I know for a fact the text in each value of the array is exactly the same as is in the "songtitle" column of the "song" table. foreach ($songsalt AS $value) { $result = mysql_query("SELECT songtitle FROM songs WHERE songtitle='$value'") or die(mysql_error()); echo mysql_num_rows($result); } while ($row = mysql_fetch_array($result)) { echo $row['songID']; echo "<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-745667 Share on other sites More sharing options...
SundayDriver Posted January 25, 2009 Author Share Posted January 25, 2009 try this you havent got the song id selected in the select statement. might be wrong theo. <?php foreach ($songsalt AS $value) { $result = mysql_query("SELECT songtitle,songID FROM songs WHERE songtitle='$value'") or die(mysql_error()); } while ($row = mysql_fetch_array($result)) { echo $row['songID']; echo "<br>"; } ?> Go figure- when I ran that I got the songID for the last value in the array. (But what I was trying to do was output each of the songIDs for every value in the array.) Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-745671 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 The first three loops (in the array) are returning no rows, so their is no song called that for the first three. Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-745798 Share on other sites More sharing options...
SundayDriver Posted January 25, 2009 Author Share Posted January 25, 2009 The first three loops (in the array) are returning no rows, so their is no song called that for the first three. I understand what you're saying, and I thank you for taking the time to respond. But try explaining this! Let's say I had the following code, where $songsalt is an array. Position 4 in the array contains the text value of "Eastbound Train". <?php $test1 = $songsalt[4]; echo $test1; $result = mysql_query("SELECT * FROM songs WHERE songtitle='$test1'") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo $row['songID']; } ?> The output of the code only produces "Eastbound Train" which is just the original echoed value before the query even takes places. It does not produce the songID. Meanwhile, back in the mySQL database, there is clearly a songID and songtitle column, with Eastbound Train as the songtitle, and 72 and the songID. That's what I don't understand. If I took '$test1' out of the query, and replaced it with 'Eastbound Train', it DOES produce the songID of 72. So what doesn't php like about my variable? Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-746112 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 Try running mysql_real_escape_string on the var first Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-746125 Share on other sites More sharing options...
SundayDriver Posted January 26, 2009 Author Share Posted January 26, 2009 Try running mysql_real_escape_string on the var first I just tried that, but still no songID output. Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-746137 Share on other sites More sharing options...
Philip Posted January 26, 2009 Share Posted January 26, 2009 <?php $test1 = $songsalt[4]; $query = "SELECT * FROM `songs` WHERE `songtitle`='$test1'"; echo $query; // does this output correctly? $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo $row['songID']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-746167 Share on other sites More sharing options...
SundayDriver Posted January 26, 2009 Author Share Posted January 26, 2009 <?php $test1 = $songsalt[4]; $query = "SELECT * FROM `songs` WHERE `songtitle`='$test1'"; echo $query; // does this output correctly? $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo $row['songID']; } ?> Hey, thanks, I didn't even think of that. Yes, the song appears title appears from that additional echo statement. Output: SELECT * FROM `songs` WHERE `songtitle`=' Eastbound Train' The songID still does appear though. I just hate not knowing whether it is the query or the while loop that is causing the issue. Hmm...I just realized there is a space in front of Eastbound... Let me "trim" it and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-746237 Share on other sites More sharing options...
Philip Posted January 26, 2009 Share Posted January 26, 2009 That space before it will throw it off. Besides trimming the space, which could lead to problems when you want the space at the beginning (not very often... but it could happen), look into where you're setting your array at. See what could be causing a space to appear there. Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-746254 Share on other sites More sharing options...
SundayDriver Posted January 26, 2009 Author Share Posted January 26, 2009 OK, problem solved. :-) The problem was my fault really- the array was created from an uploaded text file of separated values. These values had spaces between the comma and the next value. This in turn created a space before every value that was sent to the query, except the first one. For this reason, the query could not find any value in the array except the first one. Instead of trimming each one, I just removed the space in the uploaded text file between songs, and will tell the eventual users of this system to do the same. So thank you KingPhilip, Blade280891, and redarrow. Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-746258 Share on other sites More sharing options...
SundayDriver Posted January 26, 2009 Author Share Posted January 26, 2009 That space before it will throw it off. Besides trimming the space, which could lead to problems when you want the space at the beginning (not very often... but it could happen), look into where you're setting your array at. See what could be causing a space to appear there. Thanks, you and I posted at the same time. :-) Quote Link to comment https://forums.phpfreaks.com/topic/142310-solved-foreach-stops-after-first-iteration/#findComment-746260 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.