加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 将$item = mysql_fetch_assoc($stmt)更改为预准备语句样

发布时间:2020-12-13 21:45:46 所属栏目:PHP教程 来源:网络整理
导读:这段代码有效,但我想弄清楚如何更改$rose = mysql_fetch_assoc($stmt);部分为“预备声明风格”.谁知道? $rose_id = $_GET['rose_id']; //prepare the statement $stmt = $conn2-prepare("SELECT * FROM rosename LEFT JOIN rosevariety ON (rosename.variet
这段代码有效,但我想弄清楚如何更改$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>";

解决方法

如果使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读