#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    France
    Posts
    13
    Rep 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 !!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    15
    just use 'return $loc_row' in your function
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    France
    Posts
    13
    Rep Power
    0
    nice try but it does'nt work
    i have alredeady try (my first try infact)
  6. #4
  7. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    Yes, Bixy, it does work. Why don't you post HOW you tried it so we can show you what you did wrong.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    France
    Posts
    13
    Rep Power
    0
    i will try again but ...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    15
    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...
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    France
    Posts
    13
    Rep Power
    0
    yes of course
    i call the function as this:
    $my_array=function fetch_col($result,$numcol);

  14. #8
  15. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    $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);

  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    France
    Posts
    13
    Rep 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
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    15
    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>";

    ?>
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    France
    Posts
    13
    Rep 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)
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Laurens, SC ,US
    Posts
    20
    Rep 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...
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Posts
    8
    Rep 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).]

Similar Threads

  1. Uploading Files
    By vivekjain in forum ASP Programming
    Replies: 0
    Last Post: February 16th, 2004, 11:21 PM
  2. help with an isGreater function
    By qiushiming in forum C Programming
    Replies: 18
    Last Post: February 5th, 2004, 08:23 AM
  3. return pointer to array question
    By theRaginCajun in forum C Programming
    Replies: 5
    Last Post: January 29th, 2004, 05:06 PM
  4. return contents of directory in array
    By jacen6678 in forum C Programming
    Replies: 1
    Last Post: January 26th, 2004, 03:32 PM
  5. array cannot be used as a function error:
    By helz in forum C Programming
    Replies: 7
    Last Post: January 20th, 2004, 11:21 AM

IMN logo majestic logo threadwatch logo seochat tools logo