keep looping the first row

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mgiwanicki
Forum Newbie
Posts: 3
Joined: Fri Aug 28, 2009 12:31 pm

keep looping the first row

Post by mgiwanicki »

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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: keep looping the first row

Post by requinix »

getEmails() will keep getting the same email(s) over and over and over again. The loop will never stop.

Move the stuff with mysql_fetch_array outside the function. Like

Code: Select all

// ...
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
 
mysql_close();
 
return $result;
}

Code: Select all

$result = getEmails();
while($row = mysql_fetch_array($result)){
echo $row['emails'];
echo "<br />";
}
Another thing: you should NOT be connecting to the database every time you run this function. It's horribly wasteful. Connect once at the beginning of the script.

Oops, should be return $result. Thanks Darhazer
Last edited by requinix on Fri Aug 28, 2009 3:52 pm, edited 2 times in total.
mgiwanicki
Forum Newbie
Posts: 3
Joined: Fri Aug 28, 2009 12:31 pm

Re: keep looping the first row

Post by mgiwanicki »

I tried that and it gave me this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /showEmails.php on line 4

what do you mean by running the connection once? I have other functions in that functions.php file for other things.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: keep looping the first row

Post by Darhazer »

Code: Select all

return $row;
in tasairis' example should be

Code: Select all

return $result;
mgiwanicki
Forum Newbie
Posts: 3
Joined: Fri Aug 28, 2009 12:31 pm

Re: keep looping the first row

Post by mgiwanicki »

wow. that actually worked. thanks guys

can you maybe shed light on this connect once issue that tasairis' brought up. thanks
Mark
Post Reply