首页 > 解决方案 > 从准备好的语句中获取关联数组

问题描述

我正在尝试使用准备好的语句从 SQL 查询中获取关联数组。我没有尝试任何工作。在执行准备好的语句之前,我能够让它工作。我正在阅读很多相互冲突的信息以及似乎不适用于过程代码的 OO 和 PDO 信息。我将来可能会切换到其中一种风格,但我不想现在就重写整个网站。任何帮助是极大的赞赏。我有一个 item_template.php,它使用 rows['name']、rows['type'] 等读取数据。它正在工作 mysqli_fetch_assoc ...我只是无法让 fetch_assoc 与准备好的语句一起工作。

  if ($searchname == NULL) {
    echo 'You must enter something to search for!';
    }else{
     $sql = "SELECT * FROM itemdb WHERE name LIKE ?";
     $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("Location: viewresults.php?error=sqlerror2");

        exit();
    }
    else{
      $searchname = "%".$searchname."%";
      mysqli_stmt_bind_param($stmt,"s", $searchname);
      mysqli_stmt_execute($stmt);
      mysqli_stmt_store_result($stmt);

      $resultcheck = mysqli_stmt_num_rows($stmt);
      echo $resultcheck;
        if ($resultcheck == 0) {
          echo 'No results found! Try again! ' . $resultcheck;
          exit();
        }else{
          $result = mysqli_stmt_get_result($stmt);
          echo $result;

          while ($row = mysqli_fetch_assoc($stmt)) {

          include("item_template.php");
          echo "SUCCESS";
        }
    }
  }
}

标签: phpmysqlarraysmysqli

解决方案


您在数据库中使用什么编码?我使用utf8_general_ci和西里尔字母。所以

mysqli_set_charset($conn, "utf8");

$conn=mysqli_connect();帮助我之后


推荐阅读