PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 23rd, 2000, 05:42 AM
BiXY BiXY is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: France
Posts: 13 BiXY User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 !!!

Reply With Quote
  #2  
Old February 23rd, 2000, 08:08 AM
Kyuzo Kyuzo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113 Kyuzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
just use 'return $loc_row' in your function

Reply With Quote
  #3  
Old February 23rd, 2000, 08:36 AM
BiXY BiXY is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: France
Posts: 13 BiXY User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
nice try but it does'nt work
i have alredeady try (my first try infact)

Reply With Quote
  #4  
Old February 23rd, 2000, 08:37 AM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 17
Send a message via AIM to rod k
Yes, Bixy, it does work. Why don't you post HOW you tried it so we can show you what you did wrong.

Reply With Quote
  #5  
Old February 23rd, 2000, 09:25 AM
BiXY BiXY is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: France
Posts: 13 BiXY User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i will try again but ...

Reply With Quote
  #6  
Old February 23rd, 2000, 11:32 AM
Kyuzo Kyuzo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113 Kyuzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #7  
Old February 23rd, 2000, 01:05 PM
BiXY BiXY is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: France
Posts: 13 BiXY User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);


Reply With Quote
  #8  
Old February 23rd, 2000, 01:53 PM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 17
Send a message via AIM to rod k
$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);


Reply With Quote
  #9  
Old February 24th, 2000, 04:35 AM
BiXY BiXY is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: France
Posts: 13 BiXY User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #10  
Old February 24th, 2000, 08:35 AM
Kyuzo Kyuzo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113 Kyuzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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>";

?>

Reply With Quote
  #11  
Old February 25th, 2000, 04:48 AM
BiXY BiXY is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: France
Posts: 13 BiXY User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
  #12  
Old April 18th, 2000, 10:11 PM
ebryan ebryan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Laurens, SC ,US
Posts: 20 ebryan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #13  
Old April 25th, 2000, 08:58 AM
Shade Shade is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2000
Posts: 8 Shade User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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).]

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > can i return array with a function ??

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap