繁体   English   中英

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

[英]How to retrieve data from MySQL database and display value in a text field

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

我有一个名为items的表。 items我具有以下字段: item_numberdescriptionitem_idcustom1custom2custom3custom4

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

mysql_connect("####", "####", "####") or die(mysql_error());
mysql_select_db("####") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM items") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
  echo '<input type="text" value='.$row['custom1'].'><br/>'; 
}

取自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();
?>

这是我的代码

  // 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-> close(); ?>

它输出thid

我需要它仅输出所选项目的海关字段,因此我添加
$ sql =“从ospos_items中选择custom1,custom2,custom3,其中item_id ='item_id'”;

因为那是唯一的数字,但它输出0 resuls,我在想是否需要一个数组或其他东西来使它工作,但我是php新手,所以我使用item_id,因为它在表中列出

暂无
暂无

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

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