Jump to content

Correct syntax for echoing variables with values!


wright67uk

Recommended Posts

What would be the correct way to use returned sql results as variables.  I have tried the way in my script, but Im unsure of how I can echo just one of the $nt variables further on in my script. 

 

$code = $_GET['postcode'];
$shortcode = substr($code,0,2);
$query =mysql_query ("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3");
echo mysql_error();
echo "<p>The email addresses you have requested are;</p>";
while($nt=mysql_fetch_row($query)){echo "$nt[0],$nt[1],$nt[2]<br>";}
echo "<p>please use $nt[2] if you ever want to contact me</p>"; //this line doesnt work for me
?></body></html>

 

is there is a way to assign variables such as;

 

$nt[0] = $firstemail

$nt[1] = $secondemail

$nt[2] = $thirdemail

 

this way I could use each of my sql values, just as I would any other variable?

 

 

try

$code = $_GET['postcode'];
$shortcode = substr($code,0,2);
$query =mysql_query ("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3");
echo mysql_error();
echo "<p>The email addresses you have requested are;</p>";
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];

echo "$nt[0],$nt[1],$nt[2]<br>";
echo "<p>please use $nt[2] if you ever want to contact me</p>"; //this line doesnt work for me
?></body></html>

Hi BH, i tried the code that you showed me.  I like how the list function works, it's new to me.

However if I go to use eg. $thirdmail elsewhere in my script, it doesn't appear to hold a value?

 

$code = $_GET['postcode'];
$shortcode = substr($code,0,2);
$query =mysql_query ("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3");
echo mysql_error();
echo "<p>The email addresses you have requested are;</p>";
while(list($firstmail, $secondmail, $thirdmail)=mysql_fetch_row($query)){echo "{$firstmail}{$secondmail}{$thirdmail}<br>";}
echo "<br>the third email address found in your query is $thirdmail"; //my html result displays nothing in place of $thirdmail when I echo it on this line?
?></body></html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.