sudip_dg77 Posted February 1, 2008 Share Posted February 1, 2008 Hi, I am kind of new to php, and i am getting a big problem on how to execute a php for each loop. I can not get it to work any how. I have a database table with many columns. I just want to pick a particular coulmn(user_id) from the table and print all the values in that column(user_id) row wise one by one. So if my table is like this: user_id fname lname email 1256 John Gone someone@email.com 4526 Som Kya someone@email.com 1422 Sab Man someone@email.com 1236 Dadi Pet someone@email.com 1233 Bob Shak someone@email.com 4866 Mili Nan someone@email.com 1232 Sam Hu someone@email.com I just want to pick up the user_id column and keep printing the values one by one. Here is my code to do that. <?php $myDatabase = "kuuja_business"; $newline = "<br />"; $con = mysql_connect("localhost:3306","kuuja_admin","admin1"); if (!$con) { die('Could not connect to Database: ' . mysql_error()); } @mysql_select_db($myDatabase, $con) or die("Unable to select database"); $report1 = mysql_query("SELECT * FROM user"); $report2 = mysql_fetch_array($report1); @report3 = $report2['user_id']; echo "$report3"; [color=red]foreach $i (@report3)[/color]{ echo "User id: $i".$newline.$newline; } ?> Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /home/kuuja/public_html/test/test.php on line 26 Line 26 is highligted in red above. But someone it is not working, but it is not working, either it is showing different errors or doesn't pick up the values one by one appropriately. Can you help? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/88936-solved-for-each-loop-how-to-do/ Share on other sites More sharing options...
Barand Posted February 1, 2008 Share Posted February 1, 2008 The syntax is <?php foreach ($array as $value) // puts the value of each element in turn into $value { echo $value . '<br>'; } or foreach ($array as $key => $value) // puts the value of each element in turn into $value and puts the elements key into $key. { echo $value . '<br>'; } In your array in $report2 you have the fields from the first record so foreach ($report2 as $val) { echo $val . '<br/>'; } gives 1256 John Gone someone@email.com Quote Link to comment https://forums.phpfreaks.com/topic/88936-solved-for-each-loop-how-to-do/#findComment-455528 Share on other sites More sharing options...
rhodesa Posted February 1, 2008 Share Posted February 1, 2008 Yeah, but the code for getting rows out of a table is like so: <?php $myDatabase = "kuuja_business"; $newline = "<br />"; $con = mysql_connect("<don't>","<post>","<this>"); if (!$con){ die('Could not connect to Database: ' . mysql_error()); } @mysql_select_db($myDatabase, $con) or die("Unable to select database"); $result = mysql_query("SELECT * FROM user"); while($row = mysql_fetch_array($result)){ $userid = $row['user_id']; echo "User id: $userid".$newline.$newline; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88936-solved-for-each-loop-how-to-do/#findComment-455531 Share on other sites More sharing options...
Barand Posted February 1, 2008 Share Posted February 1, 2008 <?php $report1 = mysql_query("SELECT * FROM user"); $report2 = mysql_fetch_array($report1); @report3 = $report2['user_id']; This is what you posted earlier. Quote Link to comment https://forums.phpfreaks.com/topic/88936-solved-for-each-loop-how-to-do/#findComment-455539 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.