BrainPill Posted February 20, 2018 Share Posted February 20, 2018 I can not fix this script, because the output is not in sync. I have 3 arrays. 1) present column names. 2) prefix (fixed name). 3) suffix (uniqid value). Can someone explain how exactly I should create a foreach loop in a foreach loop with 3 arrays and fetching the field value from the get_result command as the the value it is related to. This would result in: <---->Present Column Name <-----> New Column Name <---->value1 <--------------------------------> value1_abcd <---->value2 <------------------------------> value2_abcd etc. <?php $prefix_arr = array( 'col_a' , 'col_b' ,'col_c' , 'col_d', 'col_e' , 'col_f' , 'col_g' , ) ; $col2 = uniqid(); $suffix = substr($col2, 5, 11); $servername = "localhost"; $username = "userx"; $password = "pass12345"; $dbname = "test_database"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $stmt = $conn->prepare("SHOW COLUMNS FROM table_xyz"); $stmt->execute(); $res = $stmt->get_result(); $pres_colname = $res; // var_dump($prefix_arr); foreach ($prefix_arr as $los_ele){ $prefix = $los_ele; // var_dump($prefix); // var_dump($suffix); $col_new = $prefix."_".$suffix; //var_dump($col_new); foreach ($pres_colname as $val){ $col = $val['Field']; // var_dump($col); ?> <br><input type="text" value =" <?php echo $col; ?>"><input type="text" value ="<?php echo $col_new;?>"> <?php $stmt = $conn->prepare("ALTER TABLE `table_xyz` CHANGE COLUMN `$col` `$col_new` VARCHAR(30) NOT NULL;"); $stmt->execute(); } } //$stmt->close(); $conn->close(); ?> The complicated thing this time is that I need the exact 'Field' value which is an output of get_result, so I can not fit it in. 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted February 20, 2018 Share Posted February 20, 2018 I'm not sure I understand. What is $prefix_arr, what's the point of it? If you're trying to get the list of column names and rename them, what's that array for? You're already getting the column names and you have a suffix, so what do you need the other array for? Link to comment Share on other sites More sharing options...
dsonesuk Posted February 22, 2018 Share Posted February 22, 2018 This is what i meant $prefix_arr = array('col_a', 'col_b', 'col_c', 'col_d', 'col_e', 'col_f', 'col_g',); $col2 = uniqid(); $suffix = substr($col2, 5, 11); $servername = "xxxxxxxx"; $username = "xxxxxx"; $password = "xxxxxxxx"; $dbname = "test_database"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } else { echo "Connection successful<br>"; } $stmt = $conn->prepare("SHOW COLUMNS FROM table_xyz"); $stmt->execute(); $res = $stmt->get_result(); $pres_colname = $res; foreach ($pres_colname as $key => $val) { $col = $val['Field']; $col_new = $prefix_arr[$key] . "_" . $suffix; ?> <br><input type="text" value ="<?php echo $col; ?>"> <input type="text" value ="<?php echo $col_new; ?>"> <?php $stmtAlter = $conn->prepare("ALTER TABLE `table_xyz` CHANGE COLUMN `$col` `$col_new` VARCHAR(30) NOT NULL;"); $stmtAlter->execute(); } $conn->close(); Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now