jimmyt1988 Posted July 31, 2009 Share Posted July 31, 2009 This code checks if username and password are entered, if so it says Welcome (usersDetails). I want it to say Welcome (firstName) and not display the other objects in the array. How do i do this?: $con = mysql_connect($server, $username, $password); mysql_select_db('distal', $con); if (!$con) { die('Could not connect: ' . mysql_error()); } $username = $_POST['username']; $password = $_POST['password']; $query = mysql_query("SELECT * FROM userRegistration WHERE userName='$username' AND password='$password'"); $result = mysql_num_rows($query); if (!$result){ echo "incorrect username or password"; } else{ echo "welcome "; while ($result = mysql_fetch_array($query,MYSQL_ASSOC)){ foreach($result as $userDetails){ echo $userDetails . "<br />"; } } } I have tried: echo "welcome "; while ($result = mysql_fetch_array($query,MYSQL_ASSOC)){ foreach($result as $userDetails){ echo $userDetails['firstName'] . "<br />"; } } and echo "welcome "; while ($result = mysql_fetch_array($query,MYSQL_ASSOC)){ foreach($result as $userDetails['firstName']){ echo $userDetails . "<br />"; } } and echo "welcome "; while ($result = mysql_fetch_array($query,MYSQL_ASSOC)){ foreach($result['firstName'] as $userDetails){ echo $userDetails . "<br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/168279-solved-displaying-a-single-result-from-array/ Share on other sites More sharing options...
kickstart Posted July 31, 2009 Share Posted July 31, 2009 Hi Like this:- $con = mysql_connect($server, $username, $password); mysql_select_db('distal', $con); if (!$con) { die('Could not connect: ' . mysql_error()); } $username = $_POST['username']; $password = $_POST['password']; $query = mysql_query("SELECT * FROM userRegistration WHERE userName='$username' AND password='$password'"); $result = mysql_num_rows($query); if (!$result){ echo "incorrect username or password"; } else{ echo "welcome "; if ($result = mysql_fetch_array($query)) echo $result['firstName'] . "<br />"; } All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/168279-solved-displaying-a-single-result-from-array/#findComment-887585 Share on other sites More sharing options...
jimmyt1988 Posted July 31, 2009 Author Share Posted July 31, 2009 thankyou very much. Super helpful forum and people ! is there a way to do this easier? else{ echo "welcome "; if ($result = mysql_fetch_array($query)){ echo $result['firstName'] . " " . $result['lastName'] . "<br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/168279-solved-displaying-a-single-result-from-array/#findComment-887587 Share on other sites More sharing options...
kickstart Posted July 31, 2009 Share Posted July 31, 2009 Hi Not really. The mysql_fetch_array brings back a single row as an array of fields. All $result['firstName'] does is ouput the 'firstName' element of the $result array. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/168279-solved-displaying-a-single-result-from-array/#findComment-887601 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.