简体   繁体   English

将来自mysql的表数据加载到一个输入字段中

[英]load table data from mysql into one input field

I'm working on trying to input data from mysql into one field, below is mysql code: what I'm trying to do is have all the data from $row['db_numbers']; 我正在尝试将mysql中的数据输入到一个字段中,下面是mysql代码:我正在尝试做的是从$ row ['db_numbers']中获取所有数据; load into the 加载到

<input type="text" name="db_numbers" values="<?php echo $row['db_numbers'];?>

but it makes double the fields 但它使田野翻了一番

were i end up with multiple db_number fields, I only want one. 如果我最终有多个db_number字段,我只想要一个。

<?php
  $query = "SELECT * FROM sms_numbers  WHERE `db_user` ='".$user_name."'";
  $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());
  while ($row = mysql_fetch_assoc($result)) { 
   $group_sms = $row['db_numbers'];
  }         
?> 

Try this: 尝试这个:

    <?php
         $query = "SELECT * FROM sms_numbers  WHERE `db_user` ='".$user_name."'";
         $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());
         WHILE($row = mysql_fetch_assoc($result)) { 
            $group_sms .= $row['db_numbers'];
         }
    ?>

And then: 接着:

     <input type="text" name="db_numbers" values="<?php echo $group_sms;?>">

Notice the '.=' in the while loop. 注意while循环中的“。=”。 It concatenates the numbers. 它连接数字。 Maybe add some spaces between them if you want ('. " "')? 如果需要,也许在它们之间添加一些空格?

    WHILE($row = mysql_fetch_assoc($result)) { 
       $group_sms .= $row['db_numbers'] . " ";
    }

Then echo the $group_sms variable since that contains all the db_numbers. 然后回显$ group_sms变量,因为它包含所有db_numbers。 Hope that helps. 希望能有所帮助。

I think, if you are directly returning $row['db_numbers'] after querying the DB, then you can just print the $group_sms in the <input /> tag. 我认为,如果您在查询数据库后直接返回$row['db_numbers'] ,则只需在<input />标记中打印$group_sms

So my opinion would be: - either return $row after querying the DB - or directly use $group_sms in <input /> tag 所以我的看法是:-查询数据库后return $row $group_sms或直接在<input />标记中使用$group_sms

If you're just trying to create an input element containing the value from a single field in the database there is no need for looping, use this: 如果您只是尝试创建一个包含来自数据库中单个字段的值的输入元素,则无需循环,请使用以下命令:

<?php
$sql = "SELECT db_numbers FROM sms_numbers WHERE username = '$user_name'";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$db_numbers = $value->id;
?>

If you have multiple values that you want to concatenate or sum you could do that directly in the query and use the same code otherwise: 如果您有多个要连接或求和的值,则可以直接在查询中执行此操作,否则使用相同的代码:

$sql = "SELECT GROUP_CONCAT(db_numbers SEPARATOR '') AS db_numbers FROM sms_numbers WHERE username = '$user_name'";
$sql = "SELECT SUM(db_numbers) AS db_numbers FROM sms_numbers WHERE username = '$user_name'";

Then use this to output: 然后使用它来输出:

<input type="text" name="db_numbers" values="<?php echo $db_numbers;?>

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

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