bind param count第一个参数

时间:2015-09-03 21:24:38

标签: php mysql mysqli prepared-statement

我有以下一行:

$formatsArray = $_POST['formats'];
$topicsArray = $_POST['topics'];


// Converting the array into individual strings
$formats = implode(",", $formatsArray);
$topics = implode(",", $topicsArray);


  // Prepare the statement
    $resources = $con->prepare("SELECT * FROM resources WHERE
            (format IN (?))
             AND (topic IN (?))");

    // Bind the statement
    $resources->bind_param('ss',$formats, $topics);

问题是从一个数组派生的主题,它可以包含多个字符串,但's'只能识别1.如果主题数组有10个条目,那么我希望如此,那就是10个,格式相同

我考虑过计算数组的大小并在每次迭代中添加一个s,但不确定如何。 任何帮助将不胜感激。

   // Count array
    $formatCount = count($formatsArray);
    $topicCount = count($topicsArray);

1 个答案:

答案 0 :(得分:1)

那么:

<?php  
    $con = new mysqli("localhost", "USERNAME", "PASSWORD", "DATABASE");

    $formatsArray = array('a','b','c','d',);
    $topicsArray = array('x','y','z',);

    $sql = 'SELECT * FROM resources WHERE (format IN (FORMAT_REPLACE_ME)) AND (topic IN (TOPIC_REPLACE_ME))';

    $formatsPlaceholders = makePlaceHolders($formatsArray);
    $topicsPlaceholders = makePlaceHolders($topicsArray);

    $sql = str_replace('FORMAT_REPLACE_ME', $formatsPlaceholders, $sql);
    $sql = str_replace('TOPIC_REPLACE_ME', $topicsPlaceholders, $sql);

    //error_log(print_r($sql,1).' '.__FILE__.' '.__LINE__,0);

    try {
        $s = $con->prepare($sql);

        $vals = array_merge($formatsArray, $topicsArray);

        // from http://stackoverflow.com/a/31562035/1814739
        $typDfs = str_repeat( 's' , count( $vals ) );
        $params = array( $typDfs );
        foreach ( $vals as $k => $v ) {
            ${ 'varvar' . $k } = $v;
            $params[] = &${ 'varvar' . $k }; # provide references
        }
        call_user_func_array( array( $s, 'bind_param' ) , $params );

        $s->execute();

        $output = array();
        $res = $s->get_result();
        while ($row = $res->fetch_array(MYSQLI_NUM))
        {
            //error_log(print_r($row,1).' '.__FILE__.' '.__LINE__,0);
            $output []= array(
                'id' => $row[0],
                'format' => $row[1],
                'topic' => $row[2],
            );
        }

        $s->close();

        sanitize_output($output);
    }
    catch (\Exception $e) {
        error_log(print_r($e->getMessage(),1).' '.__FILE__.' '.__LINE__,0);
    }

    function makePlaceHolders($arr){
        $ph = '';
        for ($i = 1; $i <= count($arr); $i++) {
            $ph .= '?,';
        }
        return rtrim($ph,',');
    }

    function sanitize_output(array &$arr, array $args=array()) {
        array_walk_recursive($arr,'so',$args);
    }
    function so(&$v,$k,$args) {
        $excludes = isset($args['excludes']) ? $args['excludes'] : array();
        if (!in_array($k,$excludes)) {
            $v = trim($v);
            $v = (get_magic_quotes_gpc()) ? stripcslashes($v) : $v;
            $v = htmlspecialchars($v);
        }
    } 
?>
<html>
<body>
    <ul>
        <?php foreach($output as $k => $o) { ?>
        <li><?php echo $o['id']; echo $o['format']; echo $o['topic']; ?></li>
        <?php } ?>
    </ul>
</body>
</html>