使用 PDO 从 MySQL 数据库中获取单个结果

Fetch a single result from a MySQL Database using PDO

好的,首先,我在 MySQL 中有一个 Table,其中包含列 "barcode"、"description" 和 "quantity"。我有两个页面,1 HTML 和 1 PHP。 HTML 中有一个表单可以接受条形码扫描或手动文本输入。基于该条目,我希望它成为 return 行,从在 HTML 表单中输入的条形码开始。到目前为止,我可以输入任何内容,它将 return 整个 table.

HTML 看起来像这样:

<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 看起来像这样:

<!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>

如果能指出正确的方向,我将不胜感激! 提前致谢!

SELECT * FROM inventoryinventory table 中选择 一切 ,没有改进或限制。我猜你想要这样的东西:

$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

问题是您完成了 SELECT * FROM inventory 而没有 WHERE 语句,因此您将从 table 获得所有内容。你需要做的是添加一个绑定参数,使用这个:

//Connection Started, Data pulled
try {
    $barcodeValue = isset($_POST['find']) ? $_POST['find'] : false;
    if($barcodeValue === false) throw new PDOException("No Barcode Value");
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM inventory WHERE `barcode` = :barcodeToSearchFor");
    $stmt->bindValue(':barcodeToSearchFor', $barcodeValue );
    $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;
    }
}