johnslater Posted September 16, 2008 Share Posted September 16, 2008 I'm using a foreach loop to output all of the information from an array. Within this while loop i have a MySQL query that is supposed to insert the outputted array into the database, each record on a new row as you would expect. Yet for some reason it refuses to write to a database at all. I know the MySQL query is because i have tried it outside of the Foreach loop and it inserts perfectly. Not sure what could cause this or whether i'm over looking someone massivly obvious. Below is the code i'm using. foreach ($feed->get_items() as $item){ $feed = $item->get_feed(); $favico = $feed->get_favicon(); $feedtitle = $feed->get_title(); $link = $item->get_permalink(); $title = $item->get_title(); $content = $item->get_content(); $date = $item->get_date('U'); $favico = mysql_real_escape_string($favico); $feedtitle = mysql_real_escape_string($feedtitle); $link = mysql_real_escape_string($link); $title = mysql_real_escape_string($title); $content = mysql_real_escape_string($content); $date = mysql_real_escape_string($date); //Determine better source name if($feedtitle == "") {$source = "";} elseif($feedtitle == "Twitter / slaterjohn") {$source = "Twitter"; $prefix = "John Just Tweeted ";} elseif($feedtitle == "digg / johnslater / history") {$source = "Digg"; $prefix = "John Just Dugg ";} elseif($feedtitle == "Johnslater's Recently Played Tracks") {$source = "Last.fm"; $prefix = "John is Listening to ";} elseif($feedtitle == "Uploads from slater.john") {$source = "Flickr"; $prefix = "John just took a new Photo called ";} elseif($feedtitle == "John Slater's Facebook Posts") {$source = "Facebook"; $prefix = "John just posted ";} elseif($feedtitle == "slater.john - Latest News") {$source = "Blog"; $prefix = "John just posted ";} elseif($feedtitle == "slater.john - Latest Reviews") {$source = "Reviews"; $prefix = "John just reviewed ";} elseif($feedtitle == "slater.john - Articles") {$source = "Articles"; $prefix = "New Article called ";} elseif($feedtitle == "YouTube :: Videos by johnslater") {$source = "YouTube"; $prefix = "John has added the video ";} $query1 = "INSERT INTO `slaterjohn`.`minifeed` (`miniID`, `feedtitle`, `itemtitle`, `itemdate`, `itemlink`, `itembody`, `favico`) VALUES (NULL , '1', '2', '3', '4', '5', '6');"; mysql_query($query1); echo $source.$title.$date.$link.$content.$favico.'<br />'; } As you can see i have even put in 1,2,3,4,5,6 for the values and not even these will insert. Something is being strange. Thanks in advanced. Link to comment https://forums.phpfreaks.com/topic/124553-php-mysql-not-inserting-rows-when-within-foreach-loop/ Share on other sites More sharing options...
peranha Posted September 16, 2008 Share Posted September 16, 2008 $query1 = "INSERT INTO `slaterjohn`.`minifeed` (`miniID`, `feedtitle`, `itemtitle`, `itemdate`, `itemlink`, `itembody`, `favico`) VALUES (NULL , '1', '2', '3', '4', '5', '6')"; mysql_query($query1); Try that, I took out the first ; in the query. you dont need it Link to comment https://forums.phpfreaks.com/topic/124553-php-mysql-not-inserting-rows-when-within-foreach-loop/#findComment-643325 Share on other sites More sharing options...
johnslater Posted September 16, 2008 Author Share Posted September 16, 2008 That doesnt make any difference. From what i can gather i believe the query is fine - i just think im missing some secret code that allows me to insert rows into a database using loops which i possible surly. Thanks for your help though :-) Link to comment https://forums.phpfreaks.com/topic/124553-php-mysql-not-inserting-rows-when-within-foreach-loop/#findComment-643330 Share on other sites More sharing options...
DarkWater Posted September 16, 2008 Share Posted September 16, 2008 Change: mysql_query($query1); To: mysql_query($query1) or die(mysql_error()); You have a MySQL syntax error somewhere, so this will show it. Please post the output. Link to comment https://forums.phpfreaks.com/topic/124553-php-mysql-not-inserting-rows-when-within-foreach-loop/#findComment-643332 Share on other sites More sharing options...
johnslater Posted September 16, 2008 Author Share Posted September 16, 2008 this is the error i get. sorry i forgot to post earlier "Lost connection to MySQL server during query" Why would the connection be lost? Link to comment https://forums.phpfreaks.com/topic/124553-php-mysql-not-inserting-rows-when-within-foreach-loop/#findComment-643338 Share on other sites More sharing options...
DarkWater Posted September 16, 2008 Share Posted September 16, 2008 Do any rows insert, or do none of them insert? Link to comment https://forums.phpfreaks.com/topic/124553-php-mysql-not-inserting-rows-when-within-foreach-loop/#findComment-643340 Share on other sites More sharing options...
johnslater Posted September 16, 2008 Author Share Posted September 16, 2008 Nothing at all inserts. But as i say this query works fine outside of the ForEach loo. Not sure why the connection would fail now. Link to comment https://forums.phpfreaks.com/topic/124553-php-mysql-not-inserting-rows-when-within-foreach-loop/#findComment-643344 Share on other sites More sharing options...
johnslater Posted September 16, 2008 Author Share Posted September 16, 2008 Solved. It appears inserting lots of rows in one go can cause MySQL to DIE! - fixed by simply lowering the number of rows per time. Link to comment https://forums.phpfreaks.com/topic/124553-php-mysql-not-inserting-rows-when-within-foreach-loop/#findComment-643351 Share on other sites More sharing options...
CroNiX Posted September 16, 2008 Share Posted September 16, 2008 That can also be adjusted in your mysql server settings. Link to comment https://forums.phpfreaks.com/topic/124553-php-mysql-not-inserting-rows-when-within-foreach-loop/#findComment-643395 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.