use*_*124 3 php sql prepared-statement
这段代码有效,但我想弄清楚如何更改$ 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>";
Run Code Online (Sandbox Code Playgroud)
如果使用PDO:
$rose = $stmt->fetch(PDO::FETCH_ASSOC);
Run Code Online (Sandbox Code Playgroud)
http://www.php.net/manual/en/pdostatement.fetch.php
如果使用mysqli:
$result = $stmt->get_result();
$rose = $result->fetch_assoc();
Run Code Online (Sandbox Code Playgroud)
http://www.php.net/manual/en/mysqli-stmt.get-result.php
http://php.net/manual/en/mysqli-result.fetch-assoc.php