GuardPH Posted September 15, 2013 Share Posted September 15, 2013 Hi... How do you insert MYSQL Query Result to Arrays $user = $_GET["user_name"]; $con=mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT data1, data2, FROM datatable"); while($row=mysql_fetch_array($result)){ echo $row['data1']; echo $row['data2']; echo $row['data3']; } # $row[] = "data1"; $row[] = "data2"; $row[] = "data2"; $xls->addRow($row); # or multidimentional array $row = array(); $row[] = array(0 =>'data1', 1=>'data2', 2=>'data3'); $xls->addRow($row); Quote Link to comment https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/ Share on other sites More sharing options...
Barand Posted September 15, 2013 Share Posted September 15, 2013 (edited) Use fetch_assoc() or fetch_row() and not fetch_array() as the latter fetches the data twice. $data = array(); $result = mysqli_query($con,"SELECT data1, data2, data3 FROM datatable"); while($row=mysqli_fetch_assoc($result)){ $data[] = $row; } Edited September 15, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/#findComment-1449555 Share on other sites More sharing options...
GuardPH Posted September 16, 2013 Author Share Posted September 16, 2013 I'm trying to insert the result of Query to an Array so that xport-xls.class.php can render the value... $data = array(); while($row=mysql_fetch_array($result)) { echo $row['data1']; echo $row['data2']; echo $row['data3']; $data[] = $row; } currently the example given is this and this is working... but said we can use data from SQL query as well so thats what i'm trying to do. Thanks! #add a multi dimension array $row = array(); $row[] = array(0 =>'Luke', 1=>'6', 2=>'2ft'); $xls->addRow($row); Quote Link to comment https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/#findComment-1449721 Share on other sites More sharing options...
AbraCadaver Posted September 16, 2013 Share Posted September 16, 2013 (edited) Should be this simple I would suspect: while($row=mysql_fetch_array($result, MYSQL_NUM)) { $xls->addRow($row); } EDIT: Or looking back at your example: while($row=mysql_fetch_array($result, MYSQL_NUM)) { $data[] = $row; } $xls->addRow($data); Not sure what addRow() is expecting. Edited September 16, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/#findComment-1449728 Share on other sites More sharing options...
GuardPH Posted September 16, 2013 Author Share Posted September 16, 2013 Oh btw, this is the addrow function public function addRow($row) { #Accepts an array or var which gets added to the spreadsheet body if(is_array($row)) { #check for multi dim array if(is_array($row[0])) { foreach($row as $key=>$array) { $this->bodyArray[] = $array; } } else { $this->bodyArray[] = $row; } } else { $this->bodyArray[][0] = $row; } } Ill try this Quote Link to comment https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/#findComment-1449736 Share on other sites More sharing options...
GuardPH Posted September 17, 2013 Author Share Posted September 17, 2013 (edited) Tried but didn't work... any idea? Edited September 17, 2013 by GuardPH Quote Link to comment https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/#findComment-1449800 Share on other sites More sharing options...
mac_gyver Posted September 17, 2013 Share Posted September 17, 2013 (edited) unless you copied the mysqli while() loop construct that Barand posted, the loop in your original code (or in post #3) won't ever work because you are mixing mysql_ and mysqli_ functions. do you have php's error_reporting set to E_ALL and display_errors set to ON so that php will help you by reporting and displaying all the errors it detects? Edited September 17, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/#findComment-1449802 Share on other sites More sharing options...
priyankagound Posted September 17, 2013 Share Posted September 17, 2013 I think you wanted to do this: while( $row = mysql_fetch_assoc( $result)){$new_array[] = $row; // Inside while loop} Or maybe store id as key too $new_array[ $row['id']] = $row; Using the second ways you would be able to address rows directly by their id, such as: $new_array[ 5]. Quote Link to comment https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/#findComment-1449813 Share on other sites More sharing options...
Barand Posted September 17, 2013 Share Posted September 17, 2013 priyankagound, keep up and read the previous reply by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/#findComment-1449835 Share on other sites More sharing options...
GuardPH Posted September 17, 2013 Author Share Posted September 17, 2013 Yes did that already... while($row=mysqli_fetch_array($result, MYSQL_NUM)) { $data[] = $row; } $xls->addRow($data); This is the Error: Undefined offset: 0 in /pathe/export-xls.class.php on line 69 public function addRow($row) { #Accepts an array or var which gets added to the spreadsheet body if(is_array($row)) { #check for multi dim array if(is_array($row[0])) { <================== line 69 foreach($row as $key=>$array) { $this->bodyArray[] = $array; } } else { $this->bodyArray[] = $row; } } else { $this->bodyArray[][0] = $row; } } Quote Link to comment https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/#findComment-1449892 Share on other sites More sharing options...
Barand Posted September 17, 2013 Share Posted September 17, 2013 try mysqli_fetch_row() instead of mysqli_fetch_assoc() Quote Link to comment https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/#findComment-1449894 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.