Skatecrazy1 Posted December 22, 2010 Share Posted December 22, 2010 Hey, was just writing rough code for a news feed script, and this error came up: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 133693565 bytes) in /Applications/XAMPP/xamppfiles/htdocs/cameo/newsfeed.php on line 17 here's the code <?php require_once("vars.php"); //write the query to get the news feed info from the database $query = "SELECT * FROM news_feed"; $result = mysqli_query($dbc, $query); //create beginning tags for the news data table $output = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" name=\"news_data\">"; //display the news feed data $row = mysqli_fetch_array($result); while($row){ $output .= "<tr>" ."<td><h3>".$row['post_title']."</h3>" ."<br />Posted by: ".$row['posted_by']." on ".$row['date']."</td>" ."</tr>" ."<tr>" ."<td>".$row['post_body']."</td>" ."</tr>"; } $output .= "</table>"; echo $output; ?> and heres my vars file if needed <? //common session variables $username = $_SESSION['username']; $is_admin = $_SESSION['is_admin']; //database connection information variables $server = "localhost"; $user = "root"; $pass = ""; $db = "cameo"; //database function variables $dbc = mysqli_connect($server, $user, $pass, $db); //http header and script return variables $index = "/cameo/index.php"; $self = $_SERVER['PHP_SELF']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/222433-weird-data-allocation-error/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 22, 2010 Share Posted December 22, 2010 while($row){ ^^^ Your code is only fetching one row from the result set but is looping over that same row in a forever loop, concatenating it to itself over and over until all available memory has been consumed. Quote Link to comment https://forums.phpfreaks.com/topic/222433-weird-data-allocation-error/#findComment-1150535 Share on other sites More sharing options...
Skatecrazy1 Posted December 23, 2010 Author Share Posted December 23, 2010 oh okay, but wouldn't while(mysql_num_rows($result)) keep fetching rows until the statement is null? Quote Link to comment https://forums.phpfreaks.com/topic/222433-weird-data-allocation-error/#findComment-1150562 Share on other sites More sharing options...
Skatecrazy1 Posted December 23, 2010 Author Share Posted December 23, 2010 umm, i got an answer as to why my code's not working, but nothing on how to fix it, i tried incrementing the row's id field as such: <?php require_once("vars.php"); //write the query to get the news feed info from the database $query = "SELECT * FROM news_feed WHERE "; $result = mysqli_query($dbc, $query); //create beginning tags for the news data table $output = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" name=\"news_data\">"; //display the news feed data while($row = mysql_fetch_array($result)){ $row_id = $row['id']; $output .= "<tr>" ."<td><h3>".$row['post_title']."</h3>" ."<br />Posted by: ".$row['posted_by']." on ".$row['date']."</td>" ."</tr>" ."<tr>" ."<td>".$row['post_body']."</td>" ."</tr>"; $row_id++; } $output .= "</table>"; echo $output; ?> now I get this error, which by the way i've been getting a lot lately and it's quite annoying, I've used the same mysql_fetch_array parameters for years and the function is just now giving me problems, happens with select_db and mysql_connect as well: Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in /Applications/XAMPP/xamppfiles/htdocs/cameo/newsfeed.php on line 10 any help would be appreciated as I have a loose deadline coming up soon on this Quote Link to comment https://forums.phpfreaks.com/topic/222433-weird-data-allocation-error/#findComment-1150873 Share on other sites More sharing options...
shlumph Posted December 23, 2010 Share Posted December 23, 2010 It's because you're mixing mysqli functions with mysql functions. while($row = mysql_fetch_array($result)){ //Should be while($row = mysqli_fetch_array($result)){ Quote Link to comment https://forums.phpfreaks.com/topic/222433-weird-data-allocation-error/#findComment-1150883 Share on other sites More sharing options...
BlueSkyIS Posted December 23, 2010 Share Posted December 23, 2010 i don't see you using $row_id anywhere, but you set it using the record and then increment it for no reason (it will be set to the next id with the next record.) if you don't need $row_id for anything, you might want to leave it out entirely. Quote Link to comment https://forums.phpfreaks.com/topic/222433-weird-data-allocation-error/#findComment-1150885 Share on other sites More sharing options...
Skatecrazy1 Posted December 23, 2010 Author Share Posted December 23, 2010 k, finally got it working, but the 2 test rows I created are being displayed twice. once again, here is the code: <?php require_once("vars.php"); //write the query to get the news feed info from the database $query = "SELECT * FROM news_feed"; $result = mysqli_query($dbc, $query); //create beginning tags for the news data table $output = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" name=\"news_data\">"; //display the news feed data while($row = mysqli_fetch_array($result)){ $output .= "<tr>" ."<td><h3>".$row['post_title']."</h3>" ."<br />Posted by: ".$row['posted_by']." on ".$row['date']."</td>" ."</tr>" ."<tr>" ."<td>".$row['post_body']."</td>" ."</tr>"; } $output .= "</table>"; echo $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/222433-weird-data-allocation-error/#findComment-1150892 Share on other sites More sharing options...
BlueSkyIS Posted December 23, 2010 Share Posted December 23, 2010 there is nothing there that would cause duplicate records to display. it sounds like you might be executing this code twice. Quote Link to comment https://forums.phpfreaks.com/topic/222433-weird-data-allocation-error/#findComment-1150897 Share on other sites More sharing options...
Skatecrazy1 Posted December 23, 2010 Author Share Posted December 23, 2010 nevermind, double checked the database and the data somehow got duplicated, thanks for all the help guys Quote Link to comment https://forums.phpfreaks.com/topic/222433-weird-data-allocation-error/#findComment-1150905 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.