简体   繁体   English

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

[英]Fetch a single result from a MySQL Database using PDO

Okay, for starters, I have a Table in MySQL with the columns "barcode","description", and "quantity". 好的,对于初学者来说,我在MySQL中有一个带有“条形码”,“描述”和“数量”列的表。 I have two pages, 1 HTML and 1 PHP. 我有两个页面,1个HTML和1个PHP。 The HTML has a form in it to accept a barcode scan, or manual text entry. HTML中包含一个接受条形码扫描或手动文本输入的表格。 Based on that entry, I want it to return a row, starting with the barcode that was entered in on the HTML form. 基于该条目,我希望它返回一行,从在HTML表单中输入的条形码开始。 So far, I can enter anything and it will return the whole table. 到目前为止,我可以输入任何内容,它将返回整个表格。

The HTML looks like this: 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>

Short, sweet and to the point! 简短,甜美而切合实际! The .PHP look like this: .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>

I'd very much appreciate a point in the right direction! 我非常感谢朝正确方向的观点! Thanks in advance! 提前致谢!

SELECT * FROM inventory selects everything from the inventory table, no refinements or limitations. SELECT * FROM inventory选择inventory表中的所有内容 ,而不进行任何细化或限制。 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 http://php.net/manual/zh/mysqli.prepare.php

The problem is you've done a SELECT * FROM inventory with no WHERE statement, so you'll get everything from that table. 问题是您没有使用WHERE语句就完成了SELECT * FROM inventory ,因此您将从该表中获取所有信息。 What you need to do is add in a binding parameter, use this: 您需要做的是添加一个绑定参数,使用此命令:

//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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM