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 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能