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? Quote Link to comment 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? Quote Link to comment 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()); Quote Link to comment Share on other sites More sharing options...
Q695 Posted December 30, 2014 Author Share Posted December 30, 2014 Also, what is $id equal to? ID as always is normally user id. printf('<br>Inserted rows: %d<br>', $q_news->rowCount()); it returns: Inserted rows: 1 Quote Link to comment Share on other sites More sharing options...
Solution Q695 Posted December 30, 2014 Author Solution Share Posted December 30, 2014 Walk away for a while, then come back, and now it works somehow Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 30, 2014 Share Posted December 30, 2014 ID as always is normally user id. Always or normally? Did the removal of the colons help? Quote Link to comment Share on other sites More sharing options...
Barand Posted December 30, 2014 Share Posted December 30, 2014 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) Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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.