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

Link to comment
Share on other sites

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!

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.