Jump to content

Recommended Posts

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);
Link to comment
https://forums.phpfreaks.com/topic/282165-mysql-query-result-to-arrays/
Share on other sites

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 by Barand

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);

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 by AbraCadaver

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

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 by mac_gyver

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].

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;
		}
	
	}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.