benma Posted September 8, 2015 Share Posted September 8, 2015 I need help with PHP basic syntax, I'm really new to the language. I'm trying to fetch data from MySQL, as you can see, 1 table 6 columns and 2 rows. +------+--------+--------+--------+-------+-----+ | first |second | third | fourth | fifth | sixth | +------+--------+--------+--------+-------+-----+ | A1 | B1 | C1 | D1 | E1 | F1 | | A2 | B2 | C2 | D2 | E2 | F2 | +------+--------+--------+--------+-------+-----+ my php file code <?php define('HOST','irrelevant'); define('USERNAME','irrelevant'); define('PASSWORD','irrelevant'); define('DB','irrelevant'); $con = mysqli_connect(HOST,USERNAME,PASSWORD,DB) or die('Unable to connect'); $sql = "select * from orders"; $res = mysqli_query($con,$sql); //while($row = mysqli_fetch_array($res)){ //array_push($result,array($row[0],$row[1],$row[2],$row[3],$row[4],$row[5])); //} mysqli_close($con); ?> All I need here is this output which is 1 array with a count(array)=2 {A1 <br> B1 <br> C1 <br> D1 <br> E1 <br> F1 , A2 <br> B2 <br> C2 <br> D2 <br> E2 <br> F2} without the <'br'> command between them all it should look like this {A1 <br'> B1 <br'> C1 <br'> D1 <br'> E1 <br'> F1 , A2 <br'> B2 <br'> C2 <br'> D2 <br'> E2 <br'> F2} in other words, I need it to turn into an array that has just 2 strings in it (the number of rows that currently exist in the table). This is probably very easy to someone who knows php good, if id'e need to implement this in java it would take me a minute but, damn this php and all these dollars! thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/298100-need-assist-with-a-simple-php-function/ Share on other sites More sharing options...
scootstah Posted September 8, 2015 Share Posted September 8, 2015 You could use implode. $array = array(); while($row = mysqli_fetch_array($res)){ $array[] = implode('<br />', $row); } Quote Link to comment https://forums.phpfreaks.com/topic/298100-need-assist-with-a-simple-php-function/#findComment-1520512 Share on other sites More sharing options...
Ch0cu3r Posted September 9, 2015 Share Posted September 9, 2015 mysqli_fetch_array by default returns both a numeric indexed array and associative array for each row So you may want to set MYSQLI_NUM as the second argument when calling mysqli_fetch_array (or use mysqli_fetch_row instead) otherwise you will end up with duplicated data. Quote Link to comment https://forums.phpfreaks.com/topic/298100-need-assist-with-a-simple-php-function/#findComment-1520538 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.