【问题标题】:How to bind param an array and a value in a query如何在查询中绑定参数数组和值
【发布时间】:2020-08-11 10:07:37
【问题描述】:

我有一个包含 n 个元素的数组,我想将它与 $genre 的值绑定。 这是我尝试过的: $流派 = $_GET['a_gen'] ?? ''; $genre 来自先前的查询,它是一个字符串。 $aTitle 也是一个字符串数组。

<?php
                    if (isset($_POST['submit-search'])) {
                        $stmt = $conn -> prepare("SELECT a_titlu, a_autor, a_text, a_data, a_imagine, a_gen FROM articol WHERE a_titlu LIKE ? OR a_text LIKE ? 
                            OR a_autor LIKE ? OR a_data LIKE ?");
                        $search ='%'.$_POST['search'].'%';
                        $stmt -> bind_param('ssss', $search, $search, $search, $search);
                        $stmt -> execute();
                        $stmt -> store_result();
                        $stmt -> bind_result($a_titlu, $a_autor, $a_text, $a_data, $a_imagine, $a_gen);
                        $stmt -> get_result();
                        $count = $stmt -> num_rows();
                        $genre = $_GET['a_gen'] ?? '';
                        $aTitle = array();
                        if ($count > 0) {
                            if($count == 1)
                                echo "<h5 class='text-center toateArt'>Am găsit ".$count." rezultat!</h5>";
                            else
                                echo "<h5 class='text-center toateArt'>Am găsit ".$count." rezultate!</h5>";
                            while ($stmt -> fetch()) {
                                array_push($aTitle, $a_titlu);
                                echo "<div class='card'>
                                        <img src=".$a_imagine." class='imgRes' />
                                        <h3 class='articleTitle'>".$a_titlu."</h3>
                                        <h6 class='articleDT'><i class='fa fa-calendar'></i>".$a_data."&nbsp<i class='fa fa-user'></i>".$a_autor."</h6>
                                        <p class='articleDescription' style='text-align: justify;'>".$a_text."</p>
                                        <div class='button_cont' align='right'>
                                            <a class='buttonA' style='text-decoration: none;' href='article.php?title=".$a_titlu."&date=".$a_data."'>Citește</a>
                                        </div>
                                    </div>
                                <br>";
                            }
                        }
                    }
                ?>
                <?php
                    $in = str_repeat('?,', count($aTitle) - 1) . '?';
                    $aTitle[] = $genre;
                    $stmt = $conn -> prepare("SELECT  a_titlu, a_data, a_gen FROM articol WHERE a_titlu NOT IN ($in) AND a_gen = ?");
                    $types = str_repeat('s', count($aTitle));
                    $stmt -> bind_param($types, ...$aTitle);
                    $stmt -> execute();
                    $stmt -> store_result();
                    $stmt -> bind_result($a_titlu, $a_data, $a_gen);
                    $stmt -> get_result();
                    $count = $stmt -> num_rows();

                        while ($stmt -> fetch()) {
                            echo "<div class='card'><p>".$a_titlu."</p><p>".$a_data."</p><p>".$a_gen."</p></div><br>";
                        }
                ?>

【问题讨论】:

  • 你也需要一个流派类型
  • $genre 来自上一个查询,它是一个字符串
  • 叹息*。 $类型。 ='s';
  • 这是什么$aGenre = ('s' $genre)

标签: php mysqli


【解决方案1】:

由于在 $aTitle 上使用参数解包后无法添加额外参数,因此最简单的方法是将其添加到最后的数组中。

$in = str_repeat('?,', count($aTitle) - 1) . '?';
// Now add in value
$aTitle[] = $genre;
$stmt = $conn -> prepare("SELECT  a_titlu, a_data, a_gen 
                          FROM articol 
                          WHERE a_titlu NOT IN ($in) AND a_gen = ?");
$types = str_repeat('s', count($aTitle));
$stmt -> bind_param($types, ...$aTitle);

【讨论】:

  • 我已经检查过了,它没有在数组的最后添加 $genre
  • 尝试在$aTitle[] = $genre; 之前和之后检查$aTitle
  • 现在可以使用了!似乎 $_GET['a_gen'] 是空的,这就是它不起作用的原因。我刚刚添加了这样的: $genre = $a_gen;谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 2020-02-07
  • 2012-02-09
  • 2020-09-02
相关资源
最近更新 更多