The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
can i return array with a function ??
Discuss can i return array with a function ?? in the PHP Development forum on Dev Shed. can i return array with a function ?? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 23rd, 2000, 05:42 AM
|
|
Junior Member
|
|
Join Date: Feb 2000
Location: France
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Hi,
i try to build a fetch_col function
i give in parameter the query result and the number of the col which i want to return
i fill an array with result in the function but i don't know how i can return the array
i try :
<?
function fetch_col($result,$numcol)
{
$nbr=mysql_num_rows($result);
for($cpt=0;$cpt<$nbr;$cpt++)
{
$tmp_row=mysql_fetch_row($result);
$loc_row[$cpt]=$tmp_row[$numcol];
echo "$loc_row[$cpt]<BR>n";
}
return array ($loc_row[]);
}
?>
i try all solution for the return array
i look inside doc but it return array only under this form
return array (0, 1, 2);
please help !!!
|

February 23rd, 2000, 08:08 AM
|
|
Contributing User
|
|
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
just use 'return $loc_row' in your function
|

February 23rd, 2000, 08:36 AM
|
|
Junior Member
|
|
Join Date: Feb 2000
Location: France
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
nice try but it does'nt work
i have alredeady try (my first try infact)
|

February 23rd, 2000, 08:37 AM
|
|
Apprentice Deity
|
|
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237

Time spent in forums: 4 m 8 sec
Reputation Power: 17
|
|
|
Yes, Bixy, it does work. Why don't you post HOW you tried it so we can show you what you did wrong.
|

February 23rd, 2000, 09:25 AM
|
|
Junior Member
|
|
Join Date: Feb 2000
Location: France
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
i will try again but ...
|

February 23rd, 2000, 11:32 AM
|
|
Contributing User
|
|
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
Do you have the function call on the right side of an assignment operation?
As in
$newarray=fetch_col($result, $numcol);
just using fetch_col($result, $numcol); won't magically produce a native state $loc_row array for use in your script...you have to assign the function return value to something...
|

February 23rd, 2000, 01:05 PM
|
|
Junior Member
|
|
Join Date: Feb 2000
Location: France
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
yes of course
i call the function as this:
$my_array=function fetch_col($result,$numcol);
|

February 23rd, 2000, 01:53 PM
|
|
Apprentice Deity
|
|
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237

Time spent in forums: 4 m 8 sec
Reputation Power: 17
|
|
|
$my_array=function .... ?
You don't include the keyword 'function' when you call the function.
function func_name($args)
{
//... code....
return $something;
}
Then you call it like
$retvalue=func_name($args);
|

February 24th, 2000, 04:35 AM
|
|
Junior Member
|
|
Join Date: Feb 2000
Location: France
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Hum .. yes sorry i don't realize what i write 
yes i call the function as this
$my_array=fetch_col($result,$num_col);

sorry
|

February 24th, 2000, 08:35 AM
|
|
Contributing User
|
|
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
Hey BiXY,
It's been bugging me (and probably rod k, too) why your script won't work so I did a little testing on my own system and got it to work. Take a look at the code and let us know if you get it working. If you can't, then it beats the heck out of me...
<?
$hostname="localhost";
$username="username";
$password="password";
$dbName="myDB";
function fetch_col($result,$numcol)
{
$nbr=mysql_num_rows($result);
for($cpt=0;$cpt<$nbr;$cpt++)
{
$tmp_row=mysql_fetch_row($result);
$loc_row[$cpt]=$tmp_row[$numcol];
}
return $loc_row;
}
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbName);
$query="select * from table_name";
$result=mysql_query($query, $link);
$list=fetch_col($result, 0);
for($i=0; $i<count($list); $i++)
print"$list[$i]<br>";
?>
|

February 25th, 2000, 04:48 AM
|
|
Junior Member
|
|
Join Date: Feb 2000
Location: France
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
I do the same !!!
but don't work ?!?
i will with your code but i thing i will got the same result
PS:i use mysql_db_query instead of mysql_query (it avoids confusion prob)
|

April 18th, 2000, 10:11 PM
|
|
Junior Member
|
|
Join Date: Feb 2000
Location: Laurens, SC ,US
Posts: 20
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
I read closely the dialoge betweem bixy, rodk and a third party (Name?) on Feb 23rd but it was just a leeeeetle bit above me.
I would like to have a function so that what is entered in the php script is something like
dbq(FieldName);
and it returns the number of rows.
function dbq($text) {
mysql_select_db("dbname");
$result=mysql_query("Select $text from TableName");
//echo "mysql_num_rows($result)";
//echo "number = $result<br>";
$number = mysql_num_rows($result);
return $number;
}
I left the // lines so you could see my various testing theories. Within the function I can echo $number or $result as 34 but back in the regular script echo "$number"; or echo "$result"; both yield blank.
From the Feb 23 dialogue, I tried using
$number = dbq($text); in the main script but got back an endless loop of lines of "0 is not a mysql result index...
Anyone know how to accomplish this? I have made Dreamweaver work so that you can click the icon and input the PHP code. Nice to work in WYSIWYG again for things like tables. But it sure would be nice to have some shortcuts for input...
|

April 25th, 2000, 08:58 AM
|
|
Registered User
|
|
Join Date: Apr 2000
Posts: 8
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by ebryan:
I would like to have a function so that what is entered in the php script is something like
dbq(FieldName);
and it returns the number of rows.
function dbq($text) {
mysql_select_db("dbname");
$result=mysql_query("Select $text from TableName");
$number = mysql_num_rows($result);
return $number;
}
... Within the function I can echo $number or $result as 34 but back in the regular script echo "$number"; or echo "$result"; both yield blank.
From the Feb 23 dialogue, I tried using
$number = dbq($text); in the main script but got back an endless loop of lines of "0 is not a mysql result index...
Anyone know how to accomplish this? [/quote]
Sure - the problem is variable scope - you can't read any variables that were created inside a function from outside the function; this is why you have a return value. You need to use the global $varname; statement at the start of your function to access variables that are set outside the function.
Here's a "tiny" example..:
<?php
$server = "localhost";
$username = "user";
$password = "password";
$db = mysql_connect($server, $username, $password);
function html_table($table, $database) {
// Fetches all contents of the given $table from
// the given $database and returns it as an HTML
// table.
// All variables inside the function belong to the
// the function and cannot be accessed. You have to
// tell the function to use an outside variable
// with the "global" statement:
global $db;
mysql_select_db($database, $db); // Select the database given in the function call.
$query = "SELECT * FROM $table"; // Build the query and then run it
$result = mysql_query($query, $db);
$return[] = "<TABLE border><TR>n"; // Open the table
// This section fetches the names of the fields
// and displays them as a row in the table.
$tmp = 0;
while ($tmp < mysql_num_fields($result)) {
$return[] = "<TH>" . mysql_field_name($result, $tmp) . "</TH>";
$tmp++;
}
echo "</TR>n";
// Now we step through each line of the result
// and then print each field in its own table cell.
while ($myline = mysql_fetch_row($result)) {
$return[] = "<TR>n"; // New row in the HTML table.
while (list($key,$value) = each($myline)) {
$return[] = " <TD>$value</TD>n"; // Show the data in an HTML table cell
}
$return[] = "</TR>n";
}
$return[] = "</TABLE>n"; // Close the HTML table
$string = implode(" ", $return); // Turn the result array into a string
return $string;
}
// Outside of the function, $query, $result,
// $myline, $key, etc don't exist.
$output = html_table("table", "database");
echo $output;
?>
Hope this helps - kinda got carried away with the example, I'm afraid. Works though :^)
[This message has been edited by Shade (edited April 25, 2000).]
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|