Jump to content

Implode Into A Table, Inside A Loop


Go to solution Solved by PFMaBiSmAd,

Recommended Posts

Im having a bit of trouble with my script. I wrote the script originaly to run from one variable which i changed manualy, but now im trying to put the whole script inside a loop. The script is now running all the way through fine on the first loop, starting starting the second but getting stuck at the part with my implode. This is my script that is around the implode area. Bear in mind that this code snippet is from a very large script.

 

$t=1;
while ($t <= $bm) {


$mn=${'m'.$t};
$mn2[$t]="$mn";


$t++;



}


// The is where the script runs to, and the error comes from the next few lines and the script stops running
$implode=implode(',', $mn2);
print $implode;
$query2="SELECT * FROM artists WHERE id IN(".implode(',', $mn2).")";
$result2=mysql_query($query2) or die(mysql_error());

$i=0;
$bm2=$bm-1;
while ($i <= $bm2) {
$id3=mysql_result($result2,$i,"id");
${"id3$i"} = $id3;
$s1=mysql_result($result2,$i,"s1");
${"s1$i"} = $s1;
$s2=mysql_result($result2,$i,"s2");
${"s2$i"} = $s2;
$sl1=mysql_result($result2,$i,"sl1");
${"sl1$i"} = $sl1;
$sl2=mysql_result($result2,$i,"sl2");
${"sl2$i"} = $sl2;


$i++;
}

 

And this is the error that im getting

 

,,,You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',,)' at line 1

 

Any help or point in the right direction would be amazing right no.

 

Thanks

Eric

Edited by ericburnard
Link to comment
https://forums.phpfreaks.com/topic/272294-implode-into-a-table-inside-a-loop/
Share on other sites

  • Solution

$mn2 contains some empty entries at the end, probably because some of your $m1, $m2, ,,, variables don't exist.

 

Why not just use an array - $m[1], $m[2], ... and avoid the extra code needed to make the $m1, $m2, ...into an array every time you use the values? By using individual $m1, $m2, ,,, variables, you must now keep track of how many of them there are so that you don't iterate past the end, which is likely what is causing your current error.

 

P.S. mysql_result is the slowest way of retrieving data from a query. And again, creating a series of name/numbered variables in your while(){} loop is taking a bunch more code to set the variables, then more code later to access the values. Just assign the rows from the query to an array.

 

BTW - variable variables are three times slower than using an array.

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.