wright67uk Posted February 9, 2011 Share Posted February 9, 2011 How can i put returned values from an sql query, into variables. If the returned values from a query were; email1@com, email2@com, email3@com how would i go about puting them into variables? $value1 = (1st returned value from my sql query, in this case it would be email1@com) $value2 = (2nd returned value from my sql query, in this case it would be email2@com) $value3 = (3rd returned value from my sql query, in this case it would be email3@com) $code = $_GET['postcode']; $message = $_GET['message']; $emailad = "[email protected]"; $shortcode = substr($code,0,2); $result = mysql_query("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3") or die(mysql_error()); echo "<h2>Business Names:</h2>"; while ($row = mysql_fetch_array( $result )) { $message .= "\r\n". $row['email'] ; } echo nl2br ($message); mail( "$emailad", "Header","$message" ); echo "<br>" . "Thank you for using our mail form."; ?> </body> </html> Thankyou for any ideas, or suggestions. Link to comment https://forums.phpfreaks.com/topic/227195-using-sql-results-as-variables/ Share on other sites More sharing options...
Leftfield Posted February 9, 2011 Share Posted February 9, 2011 $i = 0; while ($row = mysql_fetch_array( $result )) { $i++; switch($i) { case 0: $value1 = $row['Email']; break ; case 1: $value2 =$row['Email']; break; } } Link to comment https://forums.phpfreaks.com/topic/227195-using-sql-results-as-variables/#findComment-1171972 Share on other sites More sharing options...
AbraCadaver Posted February 9, 2011 Share Posted February 9, 2011 Best to use an array: while($row = mysql_fetch_assoc($result)) { $value[] = $row['Email']; } // then you can use $value[0], $value[1], etc... probably in a loop Link to comment https://forums.phpfreaks.com/topic/227195-using-sql-results-as-variables/#findComment-1171990 Share on other sites More sharing options...
wright67uk Posted February 9, 2011 Author Share Posted February 9, 2011 Thankyou for the replys. Ive tried too put this together, and i still get the correct html returned, but unfortunately no emails are sent. Have I put this together wrong? $code = $_GET['postcode']; $message = $_GET['message']; $emailad = "[email protected]"; $shortcode = substr($code,0,2); $result = mysql_query("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3") or die(mysql_error()); echo "<h2>Business Names:</h2>"; while ($row = mysql_fetch_array( $result )) { $message .= "\r\n". $row['email'] ; } $i = 0; while($row = mysql_fetch_assoc($result)) {$value[] = $row['Email'];} { $i++; switch($i) { case 0:$value1 = $row['Email']; break; case 1:$value2 =$row['Email']; break; case 2:$value3 =$row['Email']; break; }} echo nl2br ($message); mail( "$value1, $value2, $value3", "Header","$message" ); echo "<br>" . "Thank you for using our mail form."; Link to comment https://forums.phpfreaks.com/topic/227195-using-sql-results-as-variables/#findComment-1172007 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.