php调用存储过程与pdo问题

时间:2017-04-20 19:28:43

标签: php mysql stored-procedures pdo

我想知道如何使用带有pdo的php代码中的in参数来调用存储过程。

我想这样做的方法是显示从数据库加载的选择框,选择一个值,然后将该值用作我的商店过程的in参数。

首先,我的选择框有sql:

$authorSql = "SELECT * FROM author"; 
$authorQuery = $pdo->prepare($authorSql); 
$authorQuery->execute();

然后在我的HTML中:

<select name="authorid"> 
<?php

$authorid="";
while($author = $authorQuery->fetch()) { 
if ($author['id'] == $authorid) { 
//The author is currently associated to the joke, select it by default 
echo "<option value=\"{$author['id']}\" selected>{$author['name']}</option>"; 
} else { 
//The author is not currently associated to the joke 
echo "<option value=\"{$author['id']}\">{$author['name']}</option>"; 
} 
}

?> 
</select> 

<table width='80%' border=0>

    <tr bgcolor='#CCCCCC'>
        <td>Joke</td>
        <td>Author Name</td>
        <td>Update</td>
    </tr>
    <?php
$result = $pdo->query("call jokes_author(:author)");       
    while($row = $result->fetch()) {         
        echo "<tr>";
        echo "<td>".$row['joketext']."</td>";
        echo "<td>".$row['name']."</td>";
        echo "<td><a href=\"edit.php?id=$row[id]\">Edit</a> | <a href=\"delete.php?id=$row[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";        
    }

    ?>
    <a href="logout.php">Logout</a>
    </table>

商店程序的代码是:

CREATE PROCEDURE `jokes_author`(IN author VARCHAR(255))
BEGIN
SELECT joke.joketext, author.name FROM author INNER JOIN joke ON author.id = joke.authorid where name = author;
END

我不能理解的部分是如何从选择框中捕捉所选择的值并转入商店程序的调用。

-Thanks

1 个答案:

答案 0 :(得分:0)

以下列方式使用PDO:

$result = $pdo->prepare("call jokes_author(:author)");       
$result->bindParam(':author', $author);
$result->execute();