$Qry and function

hi all

i have this on my product.php


<?
if(($categy == 1) && ($dealr == 2))
	{
	$qry="select * from men";
	clothes();
	}
if(($categy == 1) && ($dealr == 3))
	{
	$qry="select * from women";
	clothes();
	}
?>	

and this in my functions.php


<?
function clothes()
{
	echo '<form action="">
	<select>
	<option>SELECT</option>';
	$result=mysql_query($qry);
	while($row=mysql_fetch_array($result))
	{
	// long code of 20 lines;
	}
	echo  '</select></form>';
}
?>

If i put the “$qry” inside the function then i dont get mysql_fetch_array error but only one query works (means i get result for either men or women based on qry inserted in function).

If i dont put “$qry” inside function then i get mysql_fetch_array error.

vineet

You will need to pass the $qry to the function. Otherwise, you would need to use global $qry; but I don’t usually recommend that.

<? 
if(($categy == 1) && ($dealr == 2)) 
    { 
    $qry="select * from men"; 
    clothes($qry); 
    } 
if(($categy == 1) && ($dealr == 3)) 
    { 
    $qry="select * from women"; 
    clothes($qry); 
    } 

function clothes($qry) 
{ 
    echo '<form action=""> 
    <select> 
    <option>SELECT</option>'; 
    $result=mysql_query($qry); 
    while($row=mysql_fetch_array($result)) 
    { 
    // long code of 20 lines; 
    } 
    echo  '</select></form>'; 
} 
?>

hi cp

thanks.
It works perfect.

I have another question :

Suppose on top of my “product page” i have these variables


$one = $_REQUEST['one'];
$two = $_REQUEST['two'];
$three = $_REQUEST['three'];

and i want echo their values inside the function, then do i have to make them all global


function clothes($qry)
{
    echo '<form action="">
    <select>
    <option>SELECT</option>';
    $result=mysql_query($qry);
    while($row=mysql_fetch_array($result))
    {
       echo $one; // echoes nothing
       echo $two; // echoes nothing
       echo $three; // echoes nothing

    // long code of 20 lines;
    }
    echo  '</select></form>';
}
?>

if i repeat those variables with $request on top of function or inside function then those values get echoes fine.


function clothes($qry)
{
$one = $_REQUEST['one'];
$two = $_REQUEST['two'];
$three = $_REQUEST['three'];
    echo '<form action="">
    <select>
    <option>SELECT</option>';
    $result=mysql_query($qry);
    while($row=mysql_fetch_array($result))
    {
       echo $one; // echoes one fine
       echo $two; // echoes two fine
       echo $three; // echoes three fine

    // long code of 20 lines;
    }
    echo  '</select></form>';
}
?>

vineet

$_REQUEST is already global, so you can just utilize that within your function. Or similar to how you passed $qry, you can pass $one, $two, and $three into your function as well.

<?
$one = $_REQUEST['one']; 
$two = $_REQUEST['two']; 
$three = $_REQUEST['three']; 
clothes($qry, $one, $two, $three);

function clothes($qry, $one, $two, $three)  
{  
    echo '<form action="">  
    <select>  
    <option>SELECT</option>';  
    $result=mysql_query($qry);  
    while($row=mysql_fetch_array($result))  
    {  
       echo $one; // echoes one fine 
       echo $two; // echoes two fine 
       echo $three; // echoes three fine 

    // long code of 20 lines;  
    }  
    echo  '</select></form>';  
}  
?>

hi cp

yes, this also works perfect.

Are values fetched from database also come under global


$qry = "select name from producttable";
$result = mysql_query($qry);
$row = mysql_fetch_array($result);
$name = $row['name'];

Will $name work as global

vineet

No, the only automatic globals are the super-globals $_REQUST, $_POST, $_GET, $_SESSION, $_COOKIE, $_SERVER, etc. Everything else, is not a global, and only has local scope.

hi cp

local scope means it will available only on that page on which its called ?

Actually i had a variable on top of my product page that is called from the database


$name = $row['name'];

and when i called “$name” inside the function on product page, it echoed the value absolutely fine.

If i will call this function on any other page then i think “$name” will echo nothing. i havent tried yet.

Thats why i asked if variable from database is also global or not.

vineet

http://php.net/manual/en/language.variables.scope.php

Actually i had a variable on top of my product page that is called from the database


$name = $row['name'];

and when i called “$name” inside the function on product page, it echoed the value absolutely fine.

If i will call this function on any other page then i think “$name” will echo nothing. i havent tried yet.

Thats why i asked if variable from database is also global or not.

vineet

You’re going to have to show me that code so I can see if it fits the description or not, as I’m not 100% certain I understand the code layout you ran to be able to answer why it would have echo’d $name.

hi cp

this is my product.php


<?php
$qry = "select name from producttable";
$result = mysql_query($qry);
$row = mysql_fetch_array($result);
$name = $row['name'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>products</title>
</head>
<body>
<?php
	if(($categy == 1) && ($dealr == 2))
	{
	$qry="select * from men";
	clothes($qry, $name);
	}
?>
</body>
</html>

this is functions.php


<?
function clothes($qry, $name)
{
    echo '<form action="">
    <select>
    <option>SELECT</option>';
    $result=mysql_query($qry);
    while($row=mysql_fetch_array($result))
    {
       echo $name; // echoes product name
    // long code of 20 lines;
    }
    echo  '</select></form>';
}
?>

vineet

You passed the variable to the function, so that is why the function has access. Otherwise, it would not have had access to it, so passing it was the right decision.