0

有人可以将以下代码重写为准备好的语句吗?

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,以我目前的知识水平,这对我来说太混乱了。谢谢。

4

4 回答 4

4

你好 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 的全部 gyst 然后担心使它更容易使用!随着重复,“漫长的道路”会更多地理解代码。不要一开始就偷工减料,一旦你明白你在做什么,就削减它们!

$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 的全部内容。希望有帮助!

也看看这个。这对我帮助太大了!

我也将用作参考(有时)-该网站看起来很垃圾,但那里有关于 PDO 的质量信息。我也用这个,我发誓这是最后一个链接!因此,在此之后,任何问题都可以提出,但希望这可以成为 PDO 的一点参考指南。(希望哈哈)

于 2013-04-18T00:14:02.433 回答
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绑定到$_POST['note']第 2 行中的(或任何其他变量)。

如果我想使用不同的值再次运行该查询:note,我只需调用第 2 行和第 3 行。

显示结果:

foreach ($notes as $note) {

    echo $note['id'] . ": " . $note['text'] . "<br />";
}
于 2013-04-17T23:54:05.463 回答
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();
于 2013-04-17T23:54:43.870 回答
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);
于 2013-04-18T00:14:56.487 回答