Escaping single quotes for INSERT (XF1)

Nerbert

Active member
I had a vBulletin product Helpfulanswers for rating posts that I recoded for XF. I'm working on rebuilding the table and adding a username field with the following query that reads from one table and inserts into another

PHP:
        $return = $this->_getDb()->query('
            INSERT INTO vbh_restore(
                helpfulanswerid,
                postid,
                poster,
                userid,
                username,
                yesno,
                dateline,
                threadid
            )
            SELECT
                h.helpfulanswerid,
                h.postid,
                IFNULL( p.user_id, 0),
                h.userid,
                IFNULL( u.username, "deleted"),
                h.yesno,
                h.dateline,
                h.threadid
            FROM vbhelp_save_2 AS h
            LEFT JOIN xf_post AS p
                ON (h.postid = p.post_id)
            LEFT JOIN xf_user AS u
                ON (u.user_id = h.userid)
            WHERE h.helpfulanswerid > ' . $start . '
            ORDER BY h.helpfulanswerid
            LIMIT 100
        ');

I've done something similar in the past but used PHP looping to insert one row at a time using datawriter. But here I want to do it more efficiently. I've run through a few batches and the query works but I know I have some usernames with apostrophes and I need to escape them. I can't find an SQL escape function. But maybe the usernames are stored in the database in some clean way such that this isn't a problem.

Alternatively, the right thing to do is use the datawriter but I don't know how to insert multiple rows. Is there an example of this somewhere I can learn from?
 
Top Bottom