Jump to content

[SOLVED] function can return Array?


aliento

Recommended Posts

I am trying to build an database application. I use the array $fields[] to store the fields of the table, when i use the function into the main content it is ok but into function it doesnt return the fields! The same with the data which i  use a multy dimension array $data[$field][$num] .

 

Is it wrong to return an array from a function?

Link to comment
https://forums.phpfreaks.com/topic/113416-solved-function-can-return-array/
Share on other sites

function request_data($database,$table)
	{
		include 'settings.php';
	$fields = request_fields($database,$table); // another function
$db = mysql_connect($db_host,$db_user, $db_pass);
mysql_select_db($database,$db)  or die("Cannot select the database.<br>" . mysql_error());
$sql="SELECT * FROM $table";
$result = mysql_query($sql,$db); 
while ($row = mysql_fetch_array($result))
for($i=0;$i<count($fields);$i++)
{	$field_s = $fields[$i];
	$data[$field_s][]=$row[$field_s];
}
mysql_close;
return $data;
}

Is this possible? (to return an array?)

This is a function from my DB class that is what you are looking for.

 

<?php

public function dbGetFields($table)
{
$this->error = FALSE;
$query = "SHOW COLUMNS FROM ".$this->dbFormat($table);
$result = $this->dbQuery($query);
if(!$result) 
{
	return FALSE;
}
for($i=0;$i<mysql_num_rows($result);$i++) 
{
	$row = mysql_fetch_assoc($result);
	$fields[$i] = $row['Field'];
}
return $fields;
}

?>

 

Enjoy Let me know if it works!

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.