You are here: Re: Create new function for a select statement - TIA « PHP SQL « IT news, forums, messages
Re: Create new function for a select statement - TIA

Posted by J.O. Aho on 12/09/06 22:04

Dekka-X wrote:
> I want to call the following query from a function :
>
> $sql2 = "SELECT * from session limit 1";
> $result_thread = mysql_query($sql2, $conn);
>
> while ($row3 = @mysql_fetch_array($result_thread)){
> $sthread=$row3['thread'];
> $slevel=$row3['level'];
> }
>
>
> The above code works fine in the program.
>
> When I create a function I get the error:
> Warning: mysql_query(): supplied argument is not a valid MySQL-Link
> resource

A variable "defined" outside the function aren't accessible inside the function

example

$a="hello"

function something() {
echo $a;
}

something(); //this results in a null-output


function somethingelse($a) {
/* Supply variable value with an argument */
echo $a;
}

somethingelse($a); //this results in a "hello"


function somethingthird() {
/* We use global value from outside the function */
global $a;
echo $a;
}

somethingthird(); //this results in a "hello"


As you notice from the examples here, you are using the something() method,
which won't work, you need to either use somethingelse() or somethingthird()
and supply the $conn into your function.



> function search_session(){
> $sql2 = "SELECT * from session limit 1";
> $result_thread = mysql_query($sql2, $conn);
>
> while ($row3 =
> @mysql_fetch_array($result_thread)){
> $sthread=$row3['thread'];
> $slevel=$row3['level'];
> return $sthread;
> return $slevel;
> }
> }

Here we see another error with your function, you have two lines of return, a
function will end at the first return, the second one will never be executed.

You have three options here, how to return more than one value at the same time

/* Takes the $conn as an argument and returns a array with your values */
function search_session_retarray($conn){
$sql2 = "SELECT * from session limit 1";
$result_thread = mysql_query($sql2, $conn);

while ($row3 = @mysql_fetch_array($result_thread)){
$sthread=$row3['thread'];
$slevel=$row3['level'];
return array($sthread, $slevel);
}
}

/* function takes two arguments, first the $conn and the second the variable
where you want to save the sthread value, function returns the slevel value */
function search_session_ret($conn,&$sthread){
$sql2 = "SELECT * from session limit 1";
$result_thread = mysql_query($sql2, $conn);

while ($row3 = @mysql_fetch_array($result_thread)){
$sthread=$row3['thread'];
$slevel=$row3['level'];
return $slevel;
}
}
/* this function don't return anything, but takes three arguments, the first
is the database link, $conn value and the two last are the variables where you
want to store the sthread and slevel values */
function search_session_noret($conn,&$sthread,&$slevel){
$sql2 = "SELECT * from session limit 1";
$result_thread = mysql_query($sql2, $conn);

while ($row3 = @mysql_fetch_array($result_thread)){
$sthread=$row3['thread'];
$slevel=$row3['level'];
return ;
}
}

/* Usage of the functions */
$sarray=search_session_retarray($conn);
echo "sthread: ".$sarray[0] ."\n";
echo "slevel: ".$sarray[1] ."\n";

$level=search_session_ret($conn,$thread);
echo "sthread: ".$thread ."\n";
echo "slevel: ".$level ."\n";


search_session_noret($conn,$thread,$level);
echo "sthread: ".$thread ."\n";
echo "slevel: ".$level ."\n";


Have fun...


//Aho

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация