2012-11-19 20:10
浏览 58

使用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);
     if(preg_match_all('/'.$this->words[$i].'/i', $this->text,$matches)){

    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(){

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

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.

    解决 无用
    打赏 举报
  • dongxizhe9755 2012-11-19 20:19

    Check the return value from $stmt->execute(), if there was a problem it will return false and you should check $stmt->errorInfo() for details.

    Or else use the ERRMODE_EXCEPTION that @Madara Uchiha suggests, but if you're not already handling exceptions in your application, this can be hard to adapt to.

    Re: your update.

    You should check error status from both PDO::prepare() and PDOStatement::execute() every time you call them. The error about "Trying to get property of non-object" likely means that $stmt is actually the boolean value false instead of a valid PDOStatement object. Your call to $stmt->bindParam() fails because false is not an object, so it cannot have a bindParam() method.

    In my opinion it's much easier to pass parameters by value instead of binding variables by reference. Here's an example of both error-checking and parameters by value:

    $pdo =  DBConnection::connect();
    $sql = "INSERT INTO `tweets`( `tweet`, `tweeter_name`, `link`, `date`, `search_id`) 
        VALUES (:tweet, :tweeter_name, :link, :date, :search_id)";
    if (($stmt = $pdo->prepare($sql)) === false) {
        die(print_r($pdo->errorInfo(), true));
    $params = array(
        ':tweet'        => $tweet,
        ':tweeter_name' => $user,
        ':link'         => $link,
        ':date'         => $date,
        ':search_id'    => $search_id
    if (($status = $stmt->execute($params) === false) {
        die(print_r($stmt->errorInfo(), true));

    The error "Column 'tweeter_name' cannot be null'" that you saw in the exception means that your tweeter_name column is declared NOT NULL, but your $user variable had no value when you bound it to the :tweeter_name parameter.

    解决 无用
    打赏 举报

相关推荐 更多相似问题