【问题标题】:Change $item = mysql_fetch_assoc($stmt) to prepared statement style将 $item = mysql_fetch_assoc($stmt) 更改为准备好的语句样式
【发布时间】:2012-02-23 20:38:34
【问题描述】:

此代码有效,但我正在尝试解决如何更改 $rose = mysql_fetch_assoc($stmt);部分到“准备好的陈述风格”。有人知道吗?

$rose_id = $_GET['rose_id'];
  //prepare the statement
  $stmt = $conn2->prepare("SELECT * FROM rosename 
            LEFT JOIN rosevariety ON (rosename.variety_name = rosevariety.variety_name) 
            WHERE rose_id = ?");

            //bind the parameters
  $stmt->bind_param("i", $rose_id);

  //$sql = mysql_query($query, $conn);
  $stmt->execute();

  //was there a good response?
  if ($stmt) {

    $rose = mysql_fetch_assoc($stmt);

    //echo out rose information
    echo "<h1>".$rose['latin_name']."</h1>";
    echo "<h2>".$rose['common_name']."</h2>";

【问题讨论】:

    标签: php sql prepared-statement


    【解决方案1】:

    如果使用 PDO:

    $rose = $stmt->fetch(PDO::FETCH_ASSOC);
    

    http://www.php.net/manual/en/pdostatement.fetch.php

    如果使用mysqli:

    $result = $stmt->get_result();
    $rose = $result->fetch_assoc();
    

    http://www.php.net/manual/en/mysqli-stmt.get-result.php
    http://php.net/manual/en/mysqli-result.fetch-assoc.php

    【讨论】:

    • 这看起来像 mysqli不是 PDO
    • 是的,它的 mysqli。我如何为 mysqli 做呢?
    【解决方案2】:

    如果使用 PDO,$stmt->fetch(PDO::FETCH_ASSOC) 将是答案。

    【讨论】:

      【解决方案3】:

      http://www.php.net/manual/en/mysqli-stmt.fetch.php

      while ($rose = $stmt->fetch()) {
         //$rose = current row;
      }
      

      其实我觉得要正确使用你需要做的

      while(($rose = $stmt->fetch()) !== false){
        //$rose = current row;
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-31
        • 2014-03-13
        • 2017-03-13
        • 1970-01-01
        • 2019-07-03
        • 2015-06-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多