使用PDO从MySQL数据库获取单个结果 [英] Fetch a single result from a MySQL Database using PDO
问题描述
好的,对于初学者来说,我在MySQL中有一个表,其中包含条形码",描述"和数量"列.我有两个页面,1个HTML和1个PHP. HTML中包含一个接受条形码扫描或手动文本输入的表格.基于该条目,我希望它返回一行,从在HTML表单中输入的条形码开始.到目前为止,我可以输入任何内容,它将返回整个表格.
Okay, for starters, I have a Table in MySQL with the columns "barcode","description", and "quantity". I have two pages, 1 HTML and 1 PHP. The HTML has a form in it to accept a barcode scan, or manual text entry. Based on that entry, I want it to return a row, starting with the barcode that was entered in on the HTML form. So far, I can enter anything and it will return the whole table.
HTML看起来像这样:
The HTML looks like this:
<head>
<font color="00CC00">
<title>Search</title>
</head>
<body bgcolor="000000">
<h2>Find Product</h2>
<form name="search" method="post" action="Barcode Search.php">
<input type="text" name="find" placeholder="Click to Scan" />
<input type="submit" name="search" value="Submit" />
</form>
</body>
</html>
简短,甜美而切合实际! .PHP看起来像这样:
Short, sweet and to the point! The .PHP look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Inventory List</title>
<body bgcolor="000000">
<font color="00cc00">
<?php
//Table
echo "<table style='border: solid 1px green;'>";
echo "<tr><th>Barcode</th><th>Description</th><th>Quantity</th></tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid green;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
//Connection Info
$servername = "127.0.0.1";
$username = "XXXX";
$password = "XXXXX";
$dbname = "test";
//Connection Started, Data pulled
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM inventory ");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
//Error Check
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
// Take Text entry and fetch the SQL Row
//Kill Connection
$conn = null;
echo "</table>";
?>
</center>
</body>
</html>
我非常感谢正确方向的观点! 预先感谢!
I'd very much appreciate a point in the right direction! Thanks in advance!
推荐答案
SELECT * FROM inventory
从inventory
表中选择所有内容,没有任何改进或限制.我猜你想要这样的东西:
SELECT * FROM inventory
selects everything from the inventory
table, no refinements or limitations. I'm guessing you want something like this:
$stmt = $conn->prepare("SELECT * FROM inventory WHERE barcode = ? LIMIT 1");
$stmt->bindParam("s", $_POST["find"]); // 's' means it's a string
$stmt->execute();
http://php.net/manual/en/mysqli.prepare.php
这篇关于使用PDO从MySQL数据库获取单个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!