Jump to content

MYSQL Query Result to Arrays


GuardPH

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
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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;
		}
	
	}
Link to comment
Share on other sites

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.