doupi1532 2012-11-19 20:10
浏览 60
已采纳

使用pdo prepared语句将数据插入数据库

Inserting data into database with pdo prepared statment, doesnt work for me:

I use this function:

    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;
}

Basically, I try to loop over the values and put them into the database.. no error is given.. nothing goes into the database ..why?

This is how I connect to the database:

 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 here:

I do the same thing with a different query..but when I try to put object properties inside a variable I get an error:

$tweet= $tweet->tweet ; $user=$tweet->tweeter_name; $link= $tweet->link;

Those variables go into a query:

       $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);

I get errors like this:

Notice: Trying to get property of non-object in C:\xampp\htdocs\Twitter\demo.php on line 36

Notice: Trying to get property of non-object in C:\xampp\htdocs\Twitter\demo.php on line 37

Notice: Trying to get property of non-object in C:\xampp\htdocs\Twitter\demo.php on line 38

I can print the properties..but when allocating them to those binded variables..the above errors crop up

I get also this:

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

I checked instead like this:

    $tweet= "111111"; // $tweet->tweet ;
        $user= "22222222"; // $tweet->tweeter_name;
        $link= "3333333";  // $tweet->link;
        $date= "444444";

and it worked..for some reason it hates those object properties ?!?

This should go as input:

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

But it doesnt...?!?

  • 写回答

2条回答 默认 最新

  • doudiemei2013 2012-11-19 20:13
    关注

    Add self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after connecting.

    It would make sure PDO will throw PDOExceptions on every error, making them very easy to see. The error would then outline exactly what's wrong.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制