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
https://forums.phpfreaks.com/topic/79234-looping-from-an-array/
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
https://forums.phpfreaks.com/topic/79234-looping-from-an-array/#findComment-401099
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
https://forums.phpfreaks.com/topic/79234-looping-from-an-array/#findComment-401496
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.