如何从MySQL数据库中检索数据并在文本字段中显示值

时间:2016-02-03 14:12:18

标签: php mysql

我想从下表中检索信息并将其显示在文本字段

我有一个名为items的表。在items中,我有字段item_numberdescriptionitem_idcustom1custom2custom3,{{1} }

如何回显或打印自定义字段的值?

custom4

2 个答案:

答案 0 :(得分:0)

取自first result on Google

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sandbox";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT item_number, description, custom1, another_field FROM items";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<p>item_number: " . $row["item_number"]. "<br />description:" . $row["description"]. "<br />custom1: " . $row["custom1"] . "<br />another_field: " . $row["another_field"] . "</p>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

答案 1 :(得分:0)

这是我的代码

  // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
  if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
  } 

$sql = "SELECT custom1, custom2, custom3 FROM ospos_items  ";
$result = $conn->query($sql);

 if ($result->num_rows > 0) {
 // output data of each row
   while($row = $result->fetch_assoc()) {
    echo "custom:" . $row["custom1"]. " custom2: " .    $row["custom2"]. " custom3 :" . $row["custom3"]. "<br>";
}
 } else {
echo "0 results";
}

$ conn-&GT;关闭(); ?&GT;

并输出thid

我需要它只输出所选项目的海关字段,所以我添加
       $ sql =“SELECT custom1,custom2,custom3 FROM ospos_items where item_id ='item_id'”;

因为这是唯一的数字,但它输出0结果我在想是否需要一个数组或某些东西让它工作,但我是新的PHP,我使用了表中列出的item_id