nightkarnation Posted October 13, 2010 Share Posted October 13, 2010 Hey guys I have the following script that correctly retrieves 2 values from mysql: security_code and selected_address: The problem I have, is how can I add the retrieved values (more than 1 row) inside an array called $data $data = array(); $result = mysql_query("SELECT security_code, selected_address FROM purchase WHERE purchase_id = '412012' AND selected_address = 'General Roca 900' ORDER BY `offers_bought`.`security_code` ASC"); $cant = 0; while($row=mysql_fetch_array($result)) { array_push($data[$row['selected_address']], $row['security_code']); $cant++; } The array_push function gives me the following error: Warning: array_push() [function.array-push]: First argument should be an array in C:\wamp\www\Test\test\excelTest.php on line 24 In other words this is want I want to achieve: $data = array(array("selected_address" => $row['security_code'])); Only that this example adds only 1 retrieved row Any ideas? Thanks in advance! Cheers, Quote Link to comment https://forums.phpfreaks.com/topic/215800-array_push-inside-while/ Share on other sites More sharing options...
AbraCadaver Posted October 13, 2010 Share Posted October 13, 2010 This is easier: while($row = mysql_fetch_array($result)) { $data[] = $row; } Quote Link to comment https://forums.phpfreaks.com/topic/215800-array_push-inside-while/#findComment-1121867 Share on other sites More sharing options...
premiso Posted October 13, 2010 Share Posted October 13, 2010 Why are you using array_push? I do not think it is relevant for your scenario: while($row=mysql_fetch_array($result)) { $data[$row['selected_address']] = $row['security_code']; $cant++; } Is what I think you are looking to do. Quote Link to comment https://forums.phpfreaks.com/topic/215800-array_push-inside-while/#findComment-1121870 Share on other sites More sharing options...
nightkarnation Posted October 13, 2010 Author Share Posted October 13, 2010 First of all thanks a lot for your help guys! Premiso your exactly right! That is what I want to achieve. The problem that I am having now is coming from the function I have, to export this array to excel, please if you have time take a look: while($row=mysql_fetch_array($result)) { $data[$row['selected_address']] = $row['security_code']; } function cleanData(&$str) { $str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/\r?\n/", "\\n", $str); if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; } # filename for download $filename = "website_data_" . date('Ymd') . ".xls"; header("Content-Disposition: attachment; filename=\"$filename\""); header("Content-Type: application/vnd.ms-excel"); $flag = false; foreach($data as $row) { if(!$flag) { # display field/column names as first row echo implode("\t", array_keys($row)) . "\n"; $flag = true; } array_walk($row, 'cleanData'); echo implode("\t", array_values($row)) . "\n"; } exit; Any suggestions? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/215800-array_push-inside-while/#findComment-1121877 Share on other sites More sharing options...
sasa Posted October 14, 2010 Share Posted October 14, 2010 change while($row=mysql_fetch_array($result)) { $data[$row['selected_address']] = $row['security_code']; } to while($row=mysql_fetch_array($result. MYSQLI_ASSOC)) { $data[] = $row; } Quote Link to comment https://forums.phpfreaks.com/topic/215800-array_push-inside-while/#findComment-1122024 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.