【问题标题】:Fetch a single result from a MySQL Database using PDO使用 PDO 从 MySQL 数据库中获取单个结果
【发布时间】:2015-03-11 21:09:33
【问题描述】:

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

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>

非常感谢您指出正确的方向! 提前致谢!

【问题讨论】:

    标签: php html mysql pdo


    【解决方案1】:

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

    $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

    【讨论】:

    • 噢!更快的打字机,更好的答案!
    • 希望没有人在不发布 find 字段的情况下进入该页面
    • 谢谢!所以我只是替换现有的 $stmt=$conn 和 $stmt->execute();与此有关吗?是什么 ”?”为了?
    • @CodenameJinn 保留execute() 行(见编辑)。 ? 是您在下一行绑定的参数的占位符(请参阅文档链接)。
    • 非常感谢!我不明白语法,但现在有意义了!
    【解决方案2】:

    问题是你做了一个SELECT * FROM inventory 没有WHERE 语句,所以你会从那个表中得到所有的东西。你需要做的是添加一个绑定参数,使用这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      • 2013-08-29
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      相关资源
      最近更新 更多