variable is not showing up outside the function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
purephazer3
Forum Newbie
Posts: 6
Joined: Thu Mar 16, 2006 3:23 pm

variable is not showing up outside the function

Post 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);
?>
Last edited by purephazer3 on Mon Mar 20, 2006 3:08 pm, edited 3 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

return the variable.

Code: Select all

...
  return $selInkcolor1
}

$inkColor1 = match_sample(4);
echo $inkColor1;
purephazer3
Forum Newbie
Posts: 6
Joined: Thu Mar 16, 2006 3:23 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

return the array ($row)
purephazer3
Forum Newbie
Posts: 6
Joined: Thu Mar 16, 2006 3:23 pm

Post 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;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$row won't appear without calling the function. ;)

Code: Select all

$row = match_sample(4);
var_dump($row);
purephazer3
Forum Newbie
Posts: 6
Joined: Thu Mar 16, 2006 3:23 pm

Post 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
Post Reply