有人可以将下面的代码重写为准备好的语句吗?
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,以我目前的知识水平,这对我来说太混乱了。谢谢。
发布于 2013-04-18 08:14:02
你好,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. Hope的所有帮助!
还可以看看this。这对我帮助太大了!
我也使用this作为参考(有时)-网站看起来很垃圾,但上面有关于PDO的高质量信息。我也使用this,我发誓这是最后一个链接!因此,在这之后,任何问题都可以问,但希望这能成为PDO上的一个小小的参考指南。(希望是笑)
发布于 2013-04-18 07:54:05
这是使用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 />";
}发布于 2013-04-18 07:54:44
使用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();https://stackoverflow.com/questions/16072212
复制相似问题