與PDO準備statment將數據插入到數據庫中,我不工作:將數據插入數據庫PDO準備statment
我用這個函數:
public function get_number_of_matches(){
$stmt = $this->pdo->prepare("INSERT INTO `words`(`word_name`, `word_count`, `search_id`) VALUES (:word, :count,:searchID)");
$stmt->bindParam(':word', $word);
$stmt->bindParam(':count', $count);
$stmt->bindParam(':searchID', $search_id);
for($i=0;$i<count($this->words);$i++){
if(preg_match_all('/'.$this->words[$i].'/i', $this->text,$matches)){
$count=count($matches[0]);
$word=$this->words[$i];
$search_id=1;
$stmt->execute();
break;
}
}
return 0;
}
基本上,我試圖遍歷所有的值並把它們放入數據庫..沒有錯誤發生..什麼都沒有進入數據庫..爲什麼?
這是我如何連接到數據庫:
class DBConnection {
public static $connect;
public static function connect(){
if(!isset(self::$connect)){
try{
self::$connect=new PDO('mysql:host=localhost;dbname=tweeter', 'root', '');
}catch(Exception $ex){
echo $ex->getMessage();
}
}
return self::$connect;
}
}
UPDATE Also..see這裏:
我做同樣的事情用不同的query..but當我嘗試把對象屬性放在一個變量中我得到一個錯誤:
$ tweet = $ tweet-> tweet; $ user = $ tweet-> tweeter_name; $ link = $ tweet-> link;
這些變量進入查詢:
$pdo= DBConnection::connect();
$stmt = $pdo->prepare("INSERT INTO `tweets`(`tweet`, `tweeter_name`, `link`, `date`, `search_id`) VALUES (:tweet, :tweeter_name, :link, :date, :search_id)");
$stmt->bindParam(':tweet', $tweet);
$stmt->bindParam(':tweeter_name', $user);
$stmt->bindParam(':link', $link);
$stmt->bindParam(':date', $date);
$stmt->bindParam(':search_id', $search_id);
我得到這樣的錯誤:
Notice: Trying to get property of non-object in C:\xampp\htdocs\Twitter\demo.php on line 36
注意:試圖讓用C非對象的屬性:\ XAMPP \ htdocs中\微博\ demo.php上線37
說明:試圖獲得非對象的屬性在C:\ XAMPP \ htdocs中\微\上線demo.php 38
它們分配給上述錯誤的綁定variables..the時,我可以打印properties..but冒出
我得到這個也:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'tweeter_name' cannot be null' in C:\xampp\htdocs\Twitter\demo.php:40 Stack trace: #0 C:\xampp\htdocs\Twitter\demo.php(40): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\Twitter\demo.php on line 40
我檢查,而不是像這樣:
$tweet= "111111"; // $tweet->tweet ;
$user= "22222222"; // $tweet->tweeter_name;
$link= "3333333"; // $tweet->link;
$date= "444444";
它的工作原因是它討厭那些對象屬性?!?
這應該作爲輸入:
RT @OrganicLiveFood: Scientists Warn #EPA Over #Monsanto's #GMO Crop Failures & Dangers #prop37 #labelGMO #yeson37 http://t.co/2XhuVxO8 Doumastic TweetCaster for iOS Mon, 19 Nov 2012 20:40:55 +0000 RT @OrganicLiveFood: Scientists Warn #EPA Over #Monsanto's #GMO Crop Failures & Dangers #prop37 #labelGMO #yeson37 http://t.co/2XhuVxO8
但它不...?!?
你是否嘗試初始化你試圖綁定的變量?例如。 '$ word = 0;',就這樣存在PDO綁定的引用。 – fimas