Page 1 of 1

variable is not showing up outside the function

Posted: Mon Mar 20, 2006 1:52 pm
by purephazer3
I have the following and I only get one echo to work

Code: Select all

<?php
function match_sample($id) {
	INCLUDE('pconfig.php');
	$query = "SELECT dinkcolor1, dinkcolor2, dlining, dmotif, dtypestyle1, dtypestyle2 FROM weddefault WHERE dsample = $id";

	$result = mysql_query($query);
	$row = mysql_fetch_array( $result );
	$selInkcolor1 = $row['dinkcolor1'];
	$selInkcolor2 = $row['dinkcolor2'];
	$selLining = $row['dlining'];
	$selMotif = $row['dmotif'];
	$selTypestyle1 = $row['dtypestyle1'];
	$selTypestyle2 = $row['dtypestyle2'];

        //this one works
        echo ($selInkcolor1);
	
	}

match_sample(4);

//this one doesn't why
echo ($selInkcolor1);
?>

Posted: Mon Mar 20, 2006 1:54 pm
by feyd
return the variable.

Code: Select all

...
  return $selInkcolor1
}

$inkColor1 = match_sample(4);
echo $inkColor1;

Posted: Mon Mar 20, 2006 2:01 pm
by purephazer3
ok, but what if i want to access all of those variables in the function. Is there a quick and easy way to do that?

Posted: Mon Mar 20, 2006 2:03 pm
by feyd
return the array ($row)

Posted: Mon Mar 20, 2006 2:23 pm
by purephazer3
sorry, darn it got the first to work can't get this too.

Code: Select all

....return $row;
}
$inkColor1 = $row['dinkcolor1'];
echo $inkColor1;

Posted: Mon Mar 20, 2006 3:39 pm
by feyd
$row won't appear without calling the function. ;)

Code: Select all

$row = match_sample(4);
var_dump($row);

Posted: Mon Mar 20, 2006 3:56 pm
by purephazer3
wow, that's awsome thanks...

so I guess

$selInkcolor1 = $row['dinkcolor1'];

$selInkcolor2 = $row['dinkcolor2'];

$selLining = $row['dlining'];

$selMotif = $row['dmotif'];

$selTypestyle1 = $row['dtypestyle1'];

$selTypestyle2 = $row['dtypestyle2'];

was a big waste a code in my function...lol