sql文の配列のへバインドがうまくいきません。
php
1$cont=array(1,2,3,4,);//ここは常に変わる数値 2$count_box=5;//ここは常に変わる数値 3$inClause = substr(str_repeat(',?', count($cont)), 1); 4$sql='SELECT * FROM `table` WHERE `category` IN ('.$inClause.') ORDER BY `id` DESC limit 10 offset ?'; 5$stmt=$dbh->prepare($sql); 6$cont_box=array_merge($count_box,array($cont)); 7$stmt->execute($cont_box);//ここでエラー表示 8
こちらで表示すると
Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''5'' at line 1' in アドレス:39 Stack trace: #0 アドレス(39): PDOStatement->execute(Array) #1 {main} thrown in アドレス on line 39
39行目とは
$stmt->execute($cont_box);
こちらのと文となります。
環境は
php 5.6
phpMyAdmin 5.6.23
です。
ちなみにこちらですと問題なくエラーが出ませんので配列の受け渡しによりエラーが起きてるのかな?とは推測しております。
php
1$cont=array(1,2,3,4,);//ここは常に変わる数値 2$inClause = substr(str_repeat(',?', count($cont)), 1); 3$sql='SELECT * FROM `table` WHERE `category` IN ('.$inClause.') ORDER BY `id` DESC limit 10 offset 5'; 4$stmt=$dbh->prepare($sql); 5$stmt->execute($cont); 6
ご回答お待ちしております。
さらにここで本来のリミットをかけない状態のヒットした数を数えたいので無理やりこれの前に
php
1$cont=array(1,2,3,4,);//ここは常に変わる数値 2$count_box=5;//ここは常に変わる数値 3$inClause = substr(str_repeat(',?', count($cont)), 1); 4$dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); 5$sql='SELECT * FROM `table` WHERE `category` IN ('.$inClause.')'; 6$stmt=$dbh->prepare($sql); 7$stmt->execute($cont_box); 8$max=$stmt->rowCount();
を入れていますがこちらはひとつの文に出来ないでしょうか?
宜しくお願いいたします。
回答2件
あなたの回答
tips
プレビュー