Jump to content

[SOLVED] Foreach stops after first iteration


SundayDriver

Recommended Posts

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>";
}
}

 

 

Link to comment
Share on other sites

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>";
   }
?>

Link to comment
Share on other sites

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>";
   }
?>

Link to comment
Share on other sites

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>";
   }

 

 

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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!  8)

 

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?

Link to comment
Share on other sites

<?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'];
}
?>

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

;)

 

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.

Link to comment
Share on other sites

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.  8)

Link to comment
Share on other sites

;)

 

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. :-)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.