Jump to content

Looping from an Array?


Solarpitch

Recommended Posts

Hey Guys,

 

I am just looking for a little help in looping through an array to list DB results.

 


//This code will return all the "History" articles from the database...

function gethistory()
{
dbconnect();
	$rowA = array();

$sql = "SELECT * FROM portal_article WHERE article_cat = 'History'";

$result = mysql_query($sql) or die(mysql_error());
while(($row = mysql_fetch_row($result)) != false) {
	$rowA[] = $row;
}
return $rowA;
}

// This function is supposed to list them but it just seems to list the first value in the return

$gethistory = gethistory();

  foreach($gethistory as $e)
	  {
	  	
	  }

                 echo $gethistory[1][2];
	 echo $gethistory[2][2];
                 echo $gethistory[3][2];

// The above is an example of what I was trying. I would like to loop through the result set though so any help would be great.

Link to comment
Share on other sites

I would normally do something like this..

 

$sql = mysql_query("SELECT id, name FROM tablename");

while ($row = mysql_fetch_array($sql))

{

  echo $row['id'] . " - " . $row['id'] . "<br>";

}

 

That would display:

 

id - name

id - name

id - name

 

for all the values in the database. Not sure if this is going to help you, I do hope so :)

Link to comment
Share on other sites

In your situation i would typically use:

 

$id = $_REQUEST['id'];
$sql = mysql_query(sprintf("SELECT * FROM `users` WHERE `id` = '%d'", $id)) or die('Error: ' . mysql_error());

if($obj = mysql_fetch_object($sql))
{
  echo 'Username: ' . $obj->username;
  echo 'E-Mail Address: ' . $obj->email;
}

 

Remember this is just an example using my own scenario but it's on the same basis

Link to comment
Share on other sites

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.