Q695 Posted December 29, 2014 Share Posted December 29, 2014 $news='news'; $sql_news = "INSERT INTO `rise`.`news` ( `user` , `message` ) VALUES ( :id, :news );"; $q_news = $conn->prepare($sql_news); $q_news -> execute(array(':id'=>$id, ':news'=>$news)); print_r($q_news); echo $q_news->rowCount(); Why does it echo 1 instead of 2 when everything else on the page is turned off for processing, and it's the last thing on the page? Why do 2 inserts happen? Link to comment https://forums.phpfreaks.com/topic/293504-pdo-runs-twice-when-everything-else-is-commented-out/ Share on other sites More sharing options...
NotionCommotion Posted December 29, 2014 Share Posted December 29, 2014 I don't think it is inserting two times, but you do have a problem. Your indexes in the array should not have the colon in front of them. Also, what is $id equal to? Link to comment https://forums.phpfreaks.com/topic/293504-pdo-runs-twice-when-everything-else-is-commented-out/#findComment-1501082 Share on other sites More sharing options...
Jacques1 Posted December 29, 2014 Share Posted December 29, 2014 That “2” could come from anything, especially when you have a print_r() right in front of the echo. Make the output unambiguous, for example printf('<br>Inserted rows: %d<br>', $q_news->rowCount()); Link to comment https://forums.phpfreaks.com/topic/293504-pdo-runs-twice-when-everything-else-is-commented-out/#findComment-1501084 Share on other sites More sharing options...
Q695 Posted December 30, 2014 Author Share Posted December 30, 2014 On 12/29/2014 at 10:23 PM, NotionCommotion said: Also, what is $id equal to? ID as always is normally user id. On 12/29/2014 at 10:44 PM, Jacques1 said: printf('<br>Inserted rows: %d<br>', $q_news->rowCount()); it returns: Inserted rows: 1 Link to comment https://forums.phpfreaks.com/topic/293504-pdo-runs-twice-when-everything-else-is-commented-out/#findComment-1501108 Share on other sites More sharing options...
Q695 Posted December 30, 2014 Author Share Posted December 30, 2014 Walk away for a while, then come back, and now it works somehow Link to comment https://forums.phpfreaks.com/topic/293504-pdo-runs-twice-when-everything-else-is-commented-out/#findComment-1501110 Share on other sites More sharing options...
NotionCommotion Posted December 30, 2014 Share Posted December 30, 2014 Quote ID as always is normally user id. Always or normally? Did the removal of the colons help? Link to comment https://forums.phpfreaks.com/topic/293504-pdo-runs-twice-when-everything-else-is-commented-out/#findComment-1501118 Share on other sites More sharing options...
Barand Posted December 30, 2014 Share Posted December 30, 2014 On 12/29/2014 at 10:23 PM, NotionCommotion said: Your indexes in the array should not have the colon in front of them. Strange, then, that the examples in the manual have the colons http://php.net/manual/en/pdostatement.execute.php (examples #1, #2) Link to comment https://forums.phpfreaks.com/topic/293504-pdo-runs-twice-when-everything-else-is-commented-out/#findComment-1501129 Share on other sites More sharing options...
Jacques1 Posted December 30, 2014 Share Posted December 30, 2014 The colons are perfectly fine, nothing wrong with them. Link to comment https://forums.phpfreaks.com/topic/293504-pdo-runs-twice-when-everything-else-is-commented-out/#findComment-1501130 Share on other sites More sharing options...
NotionCommotion Posted December 30, 2014 Share Posted December 30, 2014 Thanks Barand and Jacques1, I knew about the colons when using bind, but not when using an array of insert values (named parameters). To have better forward compatibility, is it best to include them? Link to comment https://forums.phpfreaks.com/topic/293504-pdo-runs-twice-when-everything-else-is-commented-out/#findComment-1501145 Share on other sites More sharing options...
Jacques1 Posted December 30, 2014 Share Posted December 30, 2014 It doesn't matter. Since they're optional, I see no reason for adding them. Link to comment https://forums.phpfreaks.com/topic/293504-pdo-runs-twice-when-everything-else-is-commented-out/#findComment-1501147 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.