0

そこで、chrome プラグイン (このメソッドを実行するための ajax 要求を実行する) 用にこのメソッドを作成しました。実行すると、file_put_contents挿入されたものの ID が表示されますinsert ignore into songs0artist_id. 理由がわかりません...誰かが私が間違っている部分を見つけるのを手伝ってくれますか?

<?php
public function saveLyrics($artist, $title, $lyric){
    $this->db->query("insert ignore into artists (artist_name) value (:artist)", array("artist"   => $artist));
    $artist_id = (int)$this->db->insertID();
    file_put_contents(__DIR__ . "/../process/page", "artist id: $artist_id");
    //return;
    if($artist_id == 0){
        $artist_id = (int)$this->db->getOne("select artist_id from artists where artist_name = :artist", array("artist" => $artist));
    }
    if($artist_id == 0){
        return false;
    }
    $this->db->query("insert ignore into songs (artist_id, song_name) values (:aid, :title)", array("aid"    => $artist_id, "title"  => $title));
    $song_id = (int)$this->db->insertID();
    if($song_id == 0){
        $song_id = (int)$this->db->getOne("select song_id from songs where artist_id = aid and song_name = :title", array("aid"   => $artist_id, "title" => $title));
    }
}

PDO ラッパー:

<?php

/** 
 * @property PDO $pdo Description
 * @property PDOStatement $sql Description
 */
class DB{

    protected $sql = null;
    protected $pdo = null;

    public function connect(){
        $this->pdo = new PDO("mysql:dbname=envne;host=xxx", "xxx", "xxx");
    }

    public function query($query, $params = array()){
        if($this->pdo === null){
            $this->connect();
        }
        $this->sql = $this->pdo->prepare($query);
        foreach($params as $key => $value){
            $this->sql->bindParam($key, $value);
        }
        $this->sql->execute();
        if(!$this->sql)
            return false;
        return true;
    }
    public function insertID(){
        return (int)$this->pdo->lastInsertId();
    }

    public function getAll($query, $params = array()){
        $this->query($query, $params);
        return $this->sql->fetchAll();
    }

    public function getOne($query, $params = array()){
        $this->query($query, $params);
        return $this->sql->fetchColumn();
    }

}

アーティスト:

mysql> describe artists;
+-------------+------------------+------+-----+-------------------+----------------+
| Field       | Type             | Null | Key | Default           | Extra          |
+-------------+------------------+------+-----+-------------------+----------------+
| artist_id   | int(10) unsigned | NO   | PRI | NULL              | auto_increment |
| artist_name | char(50)         | YES  | UNI | NULL              |                |
| add_date    | timestamp        | YES  |     | CURRENT_TIMESTAMP |                |
+-------------+------------------+------+-----+-------------------+----------------+
3 rows in set (0.00 sec)

曲:

mysql> describe songs;
+------------+------------------+------+-----+-------------------+----------------+
| Field      | Type             | Null | Key | Default           | Extra          |
+------------+------------------+------+-----+-------------------+----------------+
| song_id    | int(10) unsigned | NO   | PRI | NULL              | auto_increment |
| artist_id  | int(11) unsigned | YES  | MUL | NULL              |                |
| album_id   | int(11)          | YES  | MUL | NULL              |                |
| song_name  | char(50)         | YES  |     | NULL              |                |
| track_id   | int(11)          | YES  |     | NULL              |                |
| date_added | timestamp        | NO   |     | CURRENT_TIMESTAMP |                |
+------------+------------------+------+-----+-------------------+----------------+
6 rows in set (0.01 sec)
4

3 に答える 3

0

Id をクエリに直接入れることにしたところ、うまくいきました。

$artist_id = (int)$this->db->insertID();
$this->db->query("insert ignore into songs (artist_id, song_name) values ($artist_id, :title)", array("title"  => $title));

機能する別の方法は、代わりに疑問符を使用することです

$artist_id = (int)$this->db->insertID();
$this->db->query("insert ignore into songs (artist_id, song_name) values (?, ?)", array($artist_id, $title));
于 2013-03-29T21:28:46.310 に答える
-1

プレースホルダーが正しく定義されていません: (コロンがありません) 私は次のようにします:

public function saveLyrics($artist, $title, $lyric){
    $this->db->query("insert ignore into artists (artist_name) value (:artist)", array(":artist"   => $artist));
    $artist_id = (int)$this->db->insertID();
    file_put_contents(__DIR__ . "/../process/page", "artist id: $artist_id");
    //return;
    if($artist_id == 0){
        $artist_id = (int)$this->db->getOne("select artist_id from artists where artist_name = :artist", array(":artist" => $artist));
        return false;
    }
    $this->db->query("insert ignore into songs (artist_id, song_name) values (:aid, :title)", array(":aid"=>$artist_id, ":title"=>$title));
    $song_id = (int)$this->db->insertID();
    if($song_id == 0){
        $song_id = (int)$this->db->getOne("select song_id from songs where artist_id = :aid and song_name = :title", array(":aid"=>$artist_id, ":title"=>$title));
    }
}

PDO ラッパーを見ると、次のコードがあります。

if(!$this->sql)
    return false;

このため、実際のエラーに気付くことはありません。この場合、エラーはプレースホルダーに関するものだと思います。($this->db->query("insert ignore into songs (...失敗した場合、クエリの実行時にエラーが発生した場合、 $song_id は false になります)。

代わりに例外を使用してエラーをキャッチしてください。

また、次のことに気付きました。

$song_id = (int)$this->db->insertID();

最初は上記のコードで、次に PDO-Wrapper の実際の関数 insertID() で、値を 2 回キャストします。多分これも考慮すべき問題です。

于 2013-03-30T07:34:59.963 に答える