We help IT Professionals succeed at work.

PHP Code modification needed (Variables in loop)

error2013 asked
Last Modified: 2013-11-14
Hi all,

First of all this is my current code:


$conn = mysqli_connect('localhost', 'admin', '', 'test');
$rs = mysqli_query($conn, "select count(*) cnt from table where field3 = 8");
$row = mysqli_fetch_assoc($rs);

$cnt = $row['cnt'];
$middle = ceil($cnt/2);

$var1 = 'string1';
$var2 = 'string2';

    "update table a,
           (SELECT @rownum:=0) b
     set article = concat(article, if((@rownum := @rownum+1) > $middle, '$var1', '$var2'))     
     where field3 = 8


Open in new window

This code above works fine but right now:

$var1 = 'string1';
$var2 = 'string2';

Open in new window

populate everything 50% 50%.

My problem here is that if the result on cnt is 10 (for example)...

At the moment 5 records will be populated with 'string1' and the other 5 records with 'string2'...


My problem is that $var1 and $var2 are going to be generated on the fly so $var1 and $var2 will be different on every run instead of now....

So it should be like this::;;

//If cnt was 10 for example...

$var1 = 'string1a';
$var2 = 'string2a';

Open in new window


update the database ...with those 2 variables above...

//then generate another 2 values...

$var1 = 'string1b';
$var2 = 'string2b';

Open in new window

update the database ...with those 2 variables above...

etc until it's reached cnt.

Note: I don't need help with generating the different $var values, I just need to have the code store different ones in a loop.

Watch Question

Most Valuable Expert 2011
Author of the Year 2014
This one is on us!
(Get your first solution completely free - no credit card required)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.