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

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.

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.