Create new function for a select statement - TIA

Create new function for a select statement - TIA

am 08.12.2006 21:03:40 von Da Dimmi de wit

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


The function code I was trying to use is here. (bonus points for
telling me what idiotic mistake I'm making) >>>.

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;
}
}

Corrected --> mysql_query(): supplied argument is not a valid MySQL-Link

am 08.12.2006 22:50:56 von Da Dimmi de wit

Your code worked perfect !!!!! No more MySQL-Link error. (and I
learned something about funtions).

function search_session_noret($conn,&$sthread,&$slevel){
$sql2 = "SELECT * from session limit 1";
$result_thread = mysql_query($sql2, $conn) or
die("Invalid query: " . mysql_error());
;

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

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

Re: Create new function for a select statement - TIA

am 09.12.2006 23:04:09 von Shion

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