Solarpitch Posted November 28, 2007 Share Posted November 28, 2007 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 More sharing options...
PhaZZed Posted November 28, 2007 Share Posted November 28, 2007 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 More sharing options...
Solarpitch Posted November 28, 2007 Author Share Posted November 28, 2007 Sorry.. but that doesnt help I am afraid ... can anyone offer a little help with this please? Link to comment https://forums.phpfreaks.com/topic/79234-looping-from-an-array/#findComment-401419 Share on other sites More sharing options...
rlindauer Posted November 28, 2007 Share Posted November 28, 2007 <?php foreach($gethistory as $e) { echo $e['field_name']; } ?> Link to comment https://forums.phpfreaks.com/topic/79234-looping-from-an-array/#findComment-401424 Share on other sites More sharing options...
Wolphie Posted November 28, 2007 Share Posted November 28, 2007 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 More sharing options...
rlindauer Posted November 28, 2007 Share Posted November 28, 2007 Oh, i am sorry. my example wasn't what you were looking for: <?php foreach($gethistory as $e) { echo $e[0]; echo $e[1]; } ?> Link to comment https://forums.phpfreaks.com/topic/79234-looping-from-an-array/#findComment-401528 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.