Simple Variable Question

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
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Simple Variable Question

Post by $var »

What is the proper syntax to get a variable from another variable, i keep getting errors?

Code: Select all

1. $email = "$wresults["Email"]"; = Error 
2. $email = "$wresults['Email']"; = Error 
3. $email = "$wresults[Email]"; = No Value 
4. $email = "Email"  = No Value
None of these will pass the e-mail value (which is there)

If someone knows how to write this properly, could you send me a line?
(Yar)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

$email = $wresults['Email'];
no need for the quotes around it, and only single quotes are needed in the element.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

nar, perhaps she's not as simple a task as i thought.
no variable is being passed.

would ye be kind enough to pillage my code?

Code: Select all

<?	
   $sql = "SELECT * FROM delegates ORDER BY Del_LName ASC";
	if(!$result = mysql_query($sql))
		 {
			 echo mysql_error();
		 } 
         while($wresults = mysql_fetch_array($result))
                { 
                } 

	if($result = mysql_query($sql))
	{
	$email = $result['Del_Email'];
        echo "<a href='mailto: $email'>";
	echo "<img size='20' height='20' border='0' src='../../../images/icons/massmail.jpg'>";
	echo "</a>";							
        }
The offending lines being:

Code: Select all

$email = $result['Del_Email'];
        echo "<a href='mailto: $email'>";
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

Code: Select all

while($wresults = mysql_fetch_array($result))
{
               
    $email = $wresults['Del_Email'];
        echo "<a href='mailto: $email'>";
    echo "<img size='20' height='20' border='0' src='../../../images/icons/massmail.jpg'>";
    echo "</a>";                            
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Your way of doing things is off

Code: Select all

$result = mysql_query("SELECT * FROM delegates ORDER BY Del_LName ASC") or die(mysql_error());
   
   while($wresults = mysql_fetch_array($result)) {
      $email = $wresults ['Del_Email'];
      echo "<a href='mailto: $email'>";
      echo "<img size='20' height='20' border='0' src='../../../images/icons/massmail.jpg'>";
      echo "</a>";                            
   }
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

i guess i should mention when i trim out the unneeded stuff...
i mean, i'm sure it is off... perhaps it looks me off, because I am drawing for dynamic dropdowns.

Code: Select all

<?	
							$cid = $_COOKIE["ID02"];
							$sql = "SELECT * FROM delegates WHERE Del_CID = $cid AND Del_Level = 0 ORDER BY Del_LName ASC";
							if(!$result = mysql_query($sql))
								{
										echo mysql_error();
								} 
							?>
							<select name="mem_1_1" class="text" id="cid">
							<? while($wresults = mysql_fetch_array($result))
							{ ?>
							<option value="<? echo $wresults["Del_ID"];?>"><? echo $wresults["Del_LName"];?>, <? echo $wresults["Del_FName"];?></option>
							<? } ?>
							</select>
					 		
							<? 
							$email = $result['Del_Email'];
							if($result = mysql_query($sql))
							{
							echo "<a href='mailto: $email'>";
							echo "<img size='20' height='20' border='0' src='../../../images/icons/massmail.jpg'>";
							echo "</a>";							
							} ?>
This is the complete code... is it really off?
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

off or not, the reason you didnt get anything for $result['Del Email'] is because $result contains only the resource id. mysql_query is not going to give you any records.

also

Code: Select all

$sql = "SELECT * FROM delegates WHERE Del_CID = $cid AND Del_Level = 0 ORDER BY Del_LName ASC";
                            if(!$result = mysql_query($sql))
                                {
                                        echo mysql_error();
                                } 

// Can be reduced to

$sql = "SELECT * FROM delegates WHERE Del_CID = $cid AND Del_Level = 0 ORDER BY Del_LName ASC";
$result = mysql_query($sql) or die(mysql_error);
Last edited by ryanlwh on Thu Sep 29, 2005 12:51 pm, edited 1 time in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

what you missing is some standard coding practices in spacing and way of doing things neatly. turning your code into somthing readable:

Code: Select all

<?    
$cid = $_COOKIE['ID02'];
$sql = "SELECT * FROM delegates WHERE Del_CID = $cid AND Del_Level = 0 ORDER BY Del_LName ASC";
$result = mysql_query($sql) or die(mysql_error());

echo '<select name="mem_1_1" class="text" id="cid">';

while($wresults = mysql_fetch_assoc($result))
{
    echo '<option value="'.$wresults['Del_ID'].'">'.$wresults['Del_LName'].','.$wresults['Del_FName'].'</option>';
}

echo '</select>';
 
$email = $result['Del_Email'];//where is this coming from? there will not be a email here unless you bring this $result from somwhere

echo '<a href="mailto: '.$email.'">';
echo '<img size="20" height="20" border="0" src="../../../images/icons/massmail.jpg">';
echo '</a>';
?>
remember that html is standard in double quotes, its best to keep it that way. also php parses single quotes faster than double so if you put the variables in a string like i just did it will parse faster
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

if(!$result = mysql_query($sql))
{
  echo mysql_error();
}
can be condensed to simply

Code: Select all

$result = mysql_query($sql) or die (mysql_error());
This is much more helpful because it terminates the scripts execution. After all is pointless to have your scripts running with false queries.
Next, in this snipplet of code:

Code: Select all

<? while($wresults = mysql_fetch_array($result))
   { ?>
      <option value="<? echo $wresults["Del_ID"];?>"><? echo $wresults["Del_LName"];?>, <? echo $wresults["Del_FName"];?></option>
<? } ?>
you are setting $wresults in the rowset of your query, but you are accessing this row `Del_Email` using $result

Code: Select all

$email =  $result['Del_Email'];

Not to mention that that last line of code is outside the loop, therefor your going to get the data of the very last row.

Not even sure what your trying to do here

Code: Select all

if($result = mysql_query($sql))
                           ?>
Why are you trying to query the data again?
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

Code: Select all

$email = $result['Del_Email'];//where is this coming from? there will not be a email here unless you bring this $result from somwhere
oops, i was fiddling and it should have read

Code: Select all

$email = $wresults['Del_Email'];//where is this coming from? there will not be a email here unless you bring this $result from somwhere
But regardless, I have not been successful in getting the variable.
But my code looks a little nicer!

Basically, what am trying to do with all of this is.

Make a graphic that links to the email of the selected value...
but that is a wish list item.

Thanks for your help though!
Post Reply