essjay_d12 Posted March 27, 2006 Share Posted March 27, 2006 i need to pull out the value 'FAVOURITES'[code]$sql = "SELECT FAVOURITES FROM users WHERE USERNAME = '$username'";[/code]how do i do this using php?thanksd Quote Link to comment https://forums.phpfreaks.com/topic/5955-how-do-i-extract-value-from-sql-query/ Share on other sites More sharing options...
toplay Posted March 27, 2006 Share Posted March 27, 2006 Checkout various tutorials. Below is an example obtained from our FAQ pinned topic at the top of the newbie topic area.I'm assuming that FAVOURITES is a column name and not a value you want to retrieve by specifying it in the "where" clause.[code]$username = 'something'; // set it first// Connect to MySQL server first – You can use variables instead of these literals$db = mysql_connect('localhost', 'username', 'password');if (!$db) { echo 'Could not connect to MySQL server. <br />Error # ', mysql_errno(), ' Error msg: ', mysql_error(); exit;}// Select the database you want to use – You can use a variable here too insteadif (!mysql_select_db('DBname', $db)) { // Did selection fail? // Handle error echo 'DB Selection failed. <br />Error # ', mysql_errno(), ' Error msg: ', mysql_error(); exit;}// An example for retrieving zero or more rows$sql = "SELECT FAVOURITES FROM `users` WHERE `USERNAME` = '$username'";$result = mysql_query($sql, $db);if (!$result) { // Handle error echo 'Query failed. SQL: ', $sql, '<br />Error # ', mysql_errno(), ' Error msg: ', mysql_error(); exit;}// The while loop stops when there's no data left; it might not even go in loop// and echo anything when there's no data returned on the first call to// mysql_fetch_assoc()while($row = mysql_fetch_assoc($result)) { // Retrieve data until no more echo $row['FAVOURITES'], '<br />';}// Optional – Free up memory. Done automatically when script endsif (!mysql_free_result($result)) { // Handle error or ignore it echo 'Freeing results failed. <br />Error # ', mysql_errno(), ' Error msg: ', mysql_error(); exit;}// Optional – Close MySQL connection. Will be closed at end of script anyway// Do NOT put a mysql_close() after a query and before fetching data!if (!mysql_close($db)) { echo "Couldn't close database <br />"; // or ignore error exit;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5955-how-do-i-extract-value-from-sql-query/#findComment-21330 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.