Jump to content

keep looping the first row


mgiwanicki

Recommended Posts

so I created a function in a separate functions.php file that looks like this:

 

function getEmails() {

global $host,$username,$password,$db_name,$tbl_name;

$tbl_name="emails"; // Table name

 

ob_start();

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$sql="SELECT * FROM $tbl_name";

$result=mysql_query($sql);

$row = mysql_fetch_array($result);

 

mysql_close();

 

return $row;

}

 

 

now on a seperate php page where I want to print all the email address I have this code:

 

<?php

include_once('functions.php');

 

while($row = getEmails()){

echo $row['emails'];

echo "<br />";

}

?>

 

this gets me the first row of my table and repeats it over and over again until I get an error saying:Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 40961 bytes)

 

can anyone help me with this? thanks

Newbie

Link to comment
Share on other sites

the code should be like this:

 

<?php
function getEmails() {
global $host,$username,$password,$db_name,$tbl_name;
$tbl_name="emails"; // Table name

ob_start();
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$emails = array();
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result)){
  $emails[] = $row[0];
}
mysql_close();

return $emails;
}
?>

 

<?php
include_once('functions.php');

foreach(getEmails() as $email){
echo $email;
echo "<br />";
}
?>

 

what is the name of the column in the emails table that you are trying to extract from...i assumed it was called 'email'

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.