有人可以将以下代码重新编写为预备声明吗?
result = mysqli_query($con,"SELECT * FROM note_system WHERE note = '$cnote'")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
$nid = $row['id'];
}
我正在尝试学习准备好的陈述,并且无法从搜索时发现的许多示例中了解它是如何工作的。我希望如果我看到一些我熟悉的代码重新编写为准备好的声明,它可能会为我点击。请不要PDO,这对我目前的知识水平来说太混乱了。谢谢。
答案 0 :(得分:4)
Hello ButterDog让我一步一步地指导您完成PDO。
步骤1)
创建一个名为connect.php的文件(或者你想要的)。每个需要数据库交互的php文件都需要此文件。
让我们开始也请注意我的评论:
?php
//We set up our database configuration
$username="xxxxx"; // Mysql username
$password="xxxxx"; // Mysql password
// Connect to server via PHP Data Object
$dbh = new PDO("mysql:host=xxxxx;dbname=xxxxx", $username, $password); // Construct the PDO variable using $dbh
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Set attributes for error reporting very IMPORTANT!
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE); // Set this to false so you can allow the actual PDO driver to do all the work, further adding abstraction to your data interactions.
?>
步骤2)需要connect.php请看一下:
require ('....../........./...../connect.php'); // Require the connect script that made your PDO variable $dbh
步骤3)
启动数据库交互只需执行以下操作,请阅读代码注释。目前我们不会担心阵列!获得完整的PDO,然后担心让它更容易使用!通过重复,“漫长的道路”可以更好地理解代码。一开始不要偷工减料,一旦你明白自己在做什么就切断它们!
$query = $dbh->prepare("SELECT * FROM note_system WHERE note = :cnote"); // This will call the variable $dbh in the required file setting up your database connection and also preparing the query!
$query->bindParam(':cnote', $cnote); // This is the bread and butter of PDO named binding, this is one of the biggest selling points of PDO! Please remember that now this step will take what ever variable ($cnote) and relate that to (:cnote)
$query->execute(); // This will then take what ever $query is execute aka run a query against the database
$row = $query->fetch(PDO::FETCH_ASSOC); // Use a simple fetch and store the variables in a array
echo $row['yourvalue']; // This will take the variable above (which is a array) and call on 'yourvalue' and then echo it.
那就是PDO.希望有所帮助!
另请查看this。这对我帮助太大了!
我还使用this作为参考(有时) - 网站看起来像垃圾,但有关于PDO的质量信息。我也使用this,我发誓这是最后一个链接!所以在此之后,任何问题都会问,但希望这可以变成PDO的一个小参考指南。 (希望大声笑)
答案 1 :(得分:1)
这是使用PDO执行此操作的一种方法:
$sel = $db->prepare("SELECT * FROM note_system WHERE note=:note");
$sel->execute(array(':note' => $_POST['note']));
$notes = $sel->fetchAll(PDO::FETCH_ASSOC);
在第1行的查询中查看占位符:note
,第2行绑定$_POST['note']
(或任何其他变量)。
如果我想再次运行该查询,使用不同的值:note
,我只需要调用第2行和第3行。
显示结果:
foreach ($notes as $note) {
echo $note['id'] . ": " . $note['text'] . "<br />";
}
答案 2 :(得分:1)
使用pdo:
http://php.net/manual/en/book.pdo.php
来自各种文档:
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = 'SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour';
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':calories' => 150, ':colour' => 'red'));
$red = $sth->fetchAll();
答案 3 :(得分:1)
这应该可以帮助你走上正确的道路......
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT id FROM note_system WHERE note = ?";
$stmt = mysqli_stmt_init($link);
if(!mysqli_stmt_prepare($stmt, $query)) {
print "Failed to prepare statement\n";
}
else {
$note = "mynote";
mysqli_stmt_bind_param($stmt, "s", $note);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result))
{
$nid = $row['id'];
}
}
mysqli_stmt_close($stmt);
mysqli_close($link);