jimmyt1988 Posted July 31, 2009 Share Posted July 31, 2009 I have been looking over and over and trying over and over to get this to work. Im seriously new at PHP, I want to echo results from SQL query... <? $con = mysql_connect($server, $username, $password); mysql_select_db('distal', $con); if (!$con) { die('Could not connect: ' . mysql_error()); } $result = mysql_query("SELECT * FROM userRegistration"); while ($row = mysql_fetch_array($result)){ echo $row; } mysql_close($con); ?> this returns: ArrayArrayArray Quote Link to comment https://forums.phpfreaks.com/topic/168259-solved-want-to-display-array-of-results/ Share on other sites More sharing options...
kickstart Posted July 31, 2009 Share Posted July 31, 2009 Hi You can use print_r, possibly combined. Personally I would use foreach:- <? $con = mysql_connect($server, $username, $password); mysql_select_db('distal', $con); if (!$con) { die('Could not connect: ' . mysql_error()); } $result = mysql_query("SELECT * FROM userRegistration"); while ($row = mysql_fetch_array($result)){ foreach($row as $oneRow) echo $oneRow; } mysql_close($con); ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/168259-solved-want-to-display-array-of-results/#findComment-887482 Share on other sites More sharing options...
jimmyt1988 Posted July 31, 2009 Author Share Posted July 31, 2009 Thankyou very much.. *grins*. However now it prints each record twice. So my output is: value1value1value2value2value3value3 instead of value1value2value3: <? $con = mysql_connect($server, $username, $password); mysql_select_db('distal', $con); if (!$con) { die('Could not connect: ' . mysql_error()); } $result = mysql_query("SELECT * FROM userRegistration"); while ($row = mysql_fetch_array($result)){ foreach($row as $oneRow) echo $oneRow . "<br />"; } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168259-solved-want-to-display-array-of-results/#findComment-887484 Share on other sites More sharing options...
kickstart Posted July 31, 2009 Share Posted July 31, 2009 Hi Can't see why that should be the case. Try this, just to check the keys and see if the issue is the data:- <? $con = mysql_connect($server, $username, $password); mysql_select_db('distal', $con); if (!$con) { die('Could not connect: ' . mysql_error()); } $result = mysql_query("SELECT * FROM userRegistration"); while ($row = mysql_fetch_array($result)){ foreach($row as $key => $oneRow) echo "$key $oneRow <br />"; } mysql_close($con); ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/168259-solved-want-to-display-array-of-results/#findComment-887488 Share on other sites More sharing options...
jimmyt1988 Posted July 31, 2009 Author Share Posted July 31, 2009 So the first without your latest addition was: firstName2 firstName2 lastName2 lastName2 userName2 userName2 password2 password2 emailAddress2 emailAddress2 dateOfBirth2 dateOfBirth2 Male Male remindPassword2 remindPassword2 6 6 firstName firstName lastName lastName userName userName password password emailAddress emailAddress dateOfBirth dateOfBirth Male Male remindPassword remindPassword 5 5 now with your latest addition: 0 firstName2 firstName firstName2 1 lastName2 lastName lastName2 2 userName2 userName userName2 3 password2 password password2 4 emailAddress2 emailAddress emailAddress2 5 dateOfBirth2 dateOfBirth dateOfBirth2 6 Male sex Male 7 remindPassword2 reminder remindPassword2 8 6 id 6 0 firstName firstName firstName 1 lastName lastName lastName 2 userName userName userName 3 password password password 4 emailAddress emailAddress emailAddress 5 dateOfBirth dateOfBirth dateOfBirth 6 Male sex Male 7 remindPassword reminder remindPassword 8 5 id 5 Quote Link to comment https://forums.phpfreaks.com/topic/168259-solved-want-to-display-array-of-results/#findComment-887491 Share on other sites More sharing options...
kickstart Posted July 31, 2009 Share Posted July 31, 2009 Hi Doh, know what the problem is. Mysql_fetch_array brings back an array of all the elements, but by default both the numeric and associative array. Try this to bring back just the associative array (or use MYSQL_NUM to just bring back the numeric array). <? $con = mysql_connect($server, $username, $password); mysql_select_db('distal', $con); if (!$con) { die('Could not connect: ' . mysql_error()); } $result = mysql_query("SELECT * FROM userRegistration"); while ($row = mysql_fetch_array($result,MYSQL_ASSOC)){ foreach($row as $key => $oneRow) echo "$key $oneRow <br />"; } mysql_close($con); ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/168259-solved-want-to-display-array-of-results/#findComment-887498 Share on other sites More sharing options...
jimmyt1988 Posted July 31, 2009 Author Share Posted July 31, 2009 Oh thankyou so much. I was reading about that thing but didnt know if i needed it because nowhere else does it put in their examples this piece of code. *sends hugs* Quote Link to comment https://forums.phpfreaks.com/topic/168259-solved-want-to-display-array-of-results/#findComment-887500 Share on other sites More sharing options...
dannyluked Posted July 31, 2009 Share Posted July 31, 2009 I would try echoing $row[COLUMN NAME] Quote Link to comment https://forums.phpfreaks.com/topic/168259-solved-want-to-display-array-of-results/#findComment-887607 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.