php - Having Multiple Values Inside One Array - For Loop? -



php - Having Multiple Values Inside One Array - For Loop? -

the script:

<?php include("connect.php"); ?> <form method="post" action="<?php echo $_server['php_self']; ?>"> <input type="text" name="name1" /> <input type="text" name="name2" /> <input type="text" name="name3" /> <input type="submit" name="submit" /> </form> <?php if(isset($_post['submit'])){ $name1 = $_post['name1']; $name2 = $_post['name2']; $name3 = $_post['name3']; $myarray = array($name1, $name2, $name3); for($i = 0; $i < count($myarray); $i++){ $tqs = "select `id` `images` `image_file` in ('" . $myarray[$i] . "')"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); $fetch_array = array(); $row = mysqli_fetch_array($tqr); $fetch_array[] = $row['id']; print_r($fetch_array); } } ?>

the script prints:

array ( [0] => 558 ) array ( [0] => 559 ) array ( [0] => 560 )

how have these values within one array?

e.g.:

array ( [0] => 558 [1] => 559 [2] => 560 )

the image file names come form , values come id column of "images" table. , looking have selected stored within one array.

the issue you're destroying array , recreating on each iteration. move line out of loop, , don't print array on each iteration:

$fetch_array = array(); // create array 1 time for($i = 0; $i < count($myarray); $i++){ $tqs = "select `id` `images` `image_file` in ('" . $myarray[$i] . "')"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); $row = mysqli_fetch_array($tqr); $fetch_array[] = $row['id']; } print_r($fetch_array); // output 1 time

side note: query vulnerable sql injection. switch prepared statement bound parameters.

php

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -