如何从mysql中选择/查询多个名称

9rygscc1  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(379)

大家好,
拜托,我正在尝试实现一些简单的东西,但我没有找到合适的解决方案在php。我想征求一些意见。
我在array/vector中有一个名为$lines的服务器列表,现在我想在mysql数据库中选择每个服务器名称,以便收集其他信息。
你能告诉我如何从中的服务器名称进行循环,选择/查询每一个吗?
谢谢!!

  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <form name="form1" method="post">
  6. <textarea rows="10" name="servers[]" cols="50" ></textarea>
  7. <input type="submit" name="submit" value="Submit" />
  8. </form>
  9. <?php
  10. $con=mysqli_connect("hostxxxx","userxxxx","xxxxxxxx","xxxxxxx");
  11. //Check connection
  12. if (mysqli_connect_errno())
  13. {
  14. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  15. }
  16. if(isset($_POST["submit"]))
  17. {
  18. if(!empty($_POST["servers"]))
  19. {
  20. echo '<h4>You have selected the following Servers:</4><br>';
  21. $submitted_array = array_keys($_POST["servers"]);
  22. $lines = explode(PHP_EOL, $_POST["servers"][$submitted_array[0]]);
  23. foreach($lines as $servers)
  24. {
  25. echo ($servers."<br>");
  26. }
  27. }
  28. else
  29. {
  30. echo 'Please write something';
  31. }
  32. }
  33. ?>
  34. </body>
  35. </html>
sdnqo3pr

sdnqo3pr1#

我得到了我需要的结果,我希望它能帮助我:

  1. <body>
  2. <form name="form1" method="post">
  3. <textarea rows="1000" name="servers[]" cols="100" style="width:300px;height:300px;"></textarea>
  4. <input type="submit" name="submit" value="Submit" />
  5. </form>
  6. <?php
  7. $con=mysqli_connect("hostxxxxx:portxx","userx","passxx","msdb");
  8. //Check connection
  9. if (mysqli_connect_errno())
  10. {
  11. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  12. }
  13. if(isset($_POST["submit"]))
  14. {
  15. if(!empty($_POST["servers"]))
  16. {
  17. echo '<h4>You have selected the following Servers:</4><br>';
  18. $submitted_array = array_keys($_POST["servers"]);
  19. //BREAKING IN AN ARRAY
  20. $lines = explode(PHP_EOL, $_POST["servers"][$submitted_array[0]]);
  21. //foreach($lines as $servers)
  22. //{
  23. //echo ($servers."<br>");
  24. //}
  25. //REMOVING BLANK SPACES:
  26. function trim_value(&$value)
  27. {
  28. $value = trim($value);
  29. }
  30. //ADDING QUOTES BETWEEN THE VALUES
  31. function add_quotes($str) {
  32. return sprintf("'%s'", $str);
  33. }
  34. //REMOVING BLANK LINES
  35. array_walk($lines, 'trim_value');
  36. $VCSAC2 = "VCSAC2";
  37. $LPAR = "LPAR";
  38. $HOSTF = "HOSTF";
  39. $CLASSG = "CLASSG";
  40. $IP = "IP";
  41. $STATUS = "STATUS";
  42. //USING IMPLODE
  43. //$sql=sprintf("SELECT * FROM main WHERE HOSTF IN (".implode(',', $lines).")");
  44. $sql=sprintf("SELECT * FROM main WHERE HOSTF IN (".implode(',', array_map('add_quotes',$lines)).")");
  45. //USED TO CHECK SQL SINTAX
  46. //echo $sql;
  47. $result=mysqli_query($con,$sql);
  48. }
  49. else
  50. {
  51. echo 'Please write something';
  52. }
  53. }
  54. ?>
  55. <table id="demo1" cellpadding="0" cellspacing="0" style="width:100%" style="font-size:13px">
  56. <thead>
  57. <tr>
  58. <th align=left>HostName</th>
  59. <th align=left>VCS</th>
  60. <th align=left>LPAR</th>
  61. <th align=left>IP</th>
  62. <th align=left>Class</th>
  63. </tr>
  64. </thead>
  65. <?php while($dado = mysqli_fetch_array($result)) {?>
  66. <tbody>
  67. <tr>
  68. <td align=left style="font-size:14px"><?php echo $dado[$HOSTF]; ?></td>
  69. <td align=left style="font-size:14px"><?php echo $dado[$VCSAC2]; ?></td>
  70. <td align=left style="font-size:14px"><?php echo $dado[$LPAR]; ?></td>
  71. <td align=left style="font-size:14px"><?php echo $dado[$IP]; ?></td>
  72. <td align=left style="font-size:14px"><?php echo $dado[$CLASSG]; ?></td>
  73. </tr>
  74. </tbody>
  75. <?php } ?>
  76. </table> <br><br>
  77. </body>
展开查看全部

相关问题