wright67uk Posted February 16, 2011 Share Posted February 16, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/227869-correct-syntax-for-echoing-variables-with-values/ Share on other sites More sharing options...
bh Posted February 16, 2011 Share Posted February 16, 2011 Hi, list function is your friend: while(list($firstmail, $secondmail, $thirdmail)=mysql_fetch_row($query)){echo "{$firstmail}, {$secondmail}, {$thirdmail}<br>";} Quote Link to comment https://forums.phpfreaks.com/topic/227869-correct-syntax-for-echoing-variables-with-values/#findComment-1175005 Share on other sites More sharing options...
Pikachu2000 Posted February 16, 2011 Share Posted February 16, 2011 Your returned array will only contain $nt[0], since you're only retrieving one field in the query. Quote Link to comment https://forums.phpfreaks.com/topic/227869-correct-syntax-for-echoing-variables-with-values/#findComment-1175017 Share on other sites More sharing options...
sasa Posted February 16, 2011 Share Posted February 16, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/227869-correct-syntax-for-echoing-variables-with-values/#findComment-1175096 Share on other sites More sharing options...
wright67uk Posted February 16, 2011 Author Share Posted February 16, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/227869-correct-syntax-for-echoing-variables-with-values/#findComment-1175255 Share on other sites More sharing options...
wright67uk Posted February 16, 2011 Author Share Posted February 16, 2011 Hi Sasa, your code works really well, thankyou to everyone that has helped me. Where was i going wrong Sasa? I noticed the $nt[] = $ntx[0]; ... what is happening here? Quote Link to comment https://forums.phpfreaks.com/topic/227869-correct-syntax-for-echoing-variables-with-values/#findComment-1175260 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.