Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/88936-solved-for-each-loop-how-to-do/
Share on other sites

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

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;
  }
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.