将$ item = mysql_fetch_assoc($ stmt)更改为预准备语句样式(Change $item = mysql_fetch_assoc($stmt) to prepared statement style)

这段代码的工作原理,但我试图找出如何更改$ 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>";

This code works but I am trying to work out how to change the $rose = mysql_fetch_assoc($stmt); section to 'prepared statement style'. Anyone know?

$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

If using PDO:

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

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

If using 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

更多推荐