PDO PHP bindValue doesn't work

后端 未结 4 413
小鲜肉
小鲜肉 2021-01-12 16:09

I know this has been asked 1000 times, but for some reason I continue to bang my head agains the wall..

This works:

$sql = \'SELECT a.eventCode, a.ev         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-12 16:41

    This indeed has been asked 1000 times.

    Prepared statements can only accept scalar values, not arbitrary parts of the SQL query.

    You have to form IN() statement using as many placeholders, as many items you have to put in and then bind them one by one.

    To ease this task one can use some helper function.

    Say, using SafeMysql library this code could be written as

    $sql  = 'SELECT * FROM events a, players b WHERE regGUID in (?a) and';
    $sql .= ' a.playerCode=b.playerCode and a.gameCode = ?s';
    $sql .= ' order by a.eventTime desc, a.actionCode asc'; 
    $results = $db->getAll($sql,$regGUID,$game);
    

    Note that $regGUID should be an array, not string and $results already contain all the requested data, without any further processing.

提交回复
热议问题