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?

 

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.