A question about return()<

windows

Guest
Hi all,

I'm kind of fooling around with some mysql stuff right now, not doing anything to serious, but theres something I'm wondering about returning values. Say I have a function in one page (select.php) and it has something like this in it...

select.php
----------

function selectdata($table,$column,$x,$y){
$sql = mysql_query("select ".$column." from ".$table." where ".$x." = ".$y.";");

while($line = mysql_fetch_array($sql,MYSQL_ASSOC)){
# I had this before and it works
# echo $line[$column];
return $line;
}
}


Can I return that $line to the main page which is calling the function? I'm a little lost on how 'return()' works in general and the real idea behind it. In my case I tried to return $line; so I could reference stuff inside it from my main script or possibly other functions. I'm not too sure that I actually understand where or why to return values, maybe somebody could help me out a little?

Thankyou, lowpro2k2Originally posted by lowpro2k2
Hi all,

I'm kind of fooling around with some mysql stuff right now, not doing anything to serious, but theres something I'm wondering about returning values. Say I have a function in one page (select.php) and it has something like this in it...

select.php
----------

function selectdata($table,$column,$x,$y){
$sql = mysql_query("select ".$column." from ".$table." where ".$x." = ".$y.";");

while($line = mysql_fetch_array($sql,MYSQL_ASSOC)){
# I had this before and it works
# echo $line[$column];
return $line;
}
}


Can I return that $line to the main page which is calling the function? I'm a little lost on how 'return()' works in general and the real idea behind it. In my case I tried to return $line; so I could reference stuff inside it from my main script or possibly other functions. I'm not too sure that I actually understand where or why to return values, maybe somebody could help me out a little?

Thankyou, lowpro2k2

return(); is generally used to set the value you want a function to set itself to once it's been run...this is normally used to set variables,or is used in echo statements...for example:



function chicken()
{
$line = 'chicken';
return $line;
}

$chicken = chicken();
echo $chicken; //echos: chicken
echo chicken(); //echos: chicken


to answer your question,yes you can...if select.php is included you can call your function as usual and have its value returned...(keep in mind your function requires an open database connection to properly run) example..lets say select.php contains the above chicken() function:



include('select.php');

$chicken = chicken();
echo $chicken; //echos: chicken
echo chicken(); //echos: chicken


but if you for example,called the function alone...ie:

chicken();


nothing would display...return doesn't 'echo' the value...it only 'sets' it.

hope this helps you a bit :)That actually helped a great deal, thanks alot :D! A few quick questions, if this was my function stored in select.php

select.php
----------

function chicken(){
$sql = mysql_query("select numEggs from chicken;");

# is the problem my while loop?
while($chickenline = mysql_fetch_array($sql,MYSQL_ASSOC)){
# does this work:
# $chickenline = mysql_fetch_array($sql,MYSQL_ASSOC)
# if theres no more than 1 line that comes back from the query?
return $chickenline;
}
}


Would I be able to access the column I selected using my mysql_query as follows?

main.php
--------

include('select.php');

$chicken = chicken();
echo $chickenline['numEggs'];no, you have to have the same name of the variable that is calling the function.


include('select.php');

$chickenline = chicken();
echo $chickenline['numEggs'];Originally posted by scoutt
no, you have to have the same name of the variable that is calling the function.


include('select.php');

$chickenline = chicken();
echo $chickenline['numEggs'];


scoutt beat me to it ;)willamouse, Scoutt, thankyou so very, very much :D :rocker: Wow, I love that feeling when something just finally works and makes sense! Appreciate it guys,

lowpro2k2actually I don't think it is ia good idea to have the return in a loop.


while($chickenline = mysql_fetch_array($sql,MYSQL_ASSOC)){
# does this work:
# $chickenline = mysql_fetch_array($sql,MYSQL_ASSOC)
# if theres no more than 1 line that comes back from the query?
return $chickenline;
}

you might get some mixed results. it might be best to put them into an array then send them out. or only use a select statement that returns 1 result. having the retun in the loop will always return the last found item, not all of them.


function chicken($id){
$sql = mysql_query("select numEggs from chicken where id = '$id';");

# is the problem my while loop?
$chickenline = mysql_fetch_array($sql,MYSQL_ASSOC);
return $chickenline;

}

include('select.php');

$chickenline = chicken($id);
echo $chickenline['numEggs'];Originally posted by lowpro2k2
willamouse, Scoutt, thankyou so very, very much :D :rocker: Wow, I love that feeling when something just finally works and makes sense! Appreciate it guys,

lowpro2k2


always glad to help :),but I'd suggest following scoutt's advice on either storing the results in an array unless your "purposely" returning a single row every time...heres an example of how my db class does it(adding the rows to an array) in case you wanna have a look(oop stuff removed and code converted to standard usage):


function get_arr_assoc()
{
$query = mysql_query("SELECT * FROM chicken");
$results = array();
while($row = mysql_fetch_array($query,MYSQL_ASSOC))
{
array_push($results,$row);
}
return $results;
}


hth :)

I was going to suggest it earlier but I wasn't sure on your intentions(one or multiple rows)
 
Back
Top