简体   繁体   English

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

[英]How to SELECT / QUERY multiple names from MySQL

Good day folks, 大家好,

Please I'm trying to implement something simple but I'm not finding the proper solution in PHP. 请我试图实现一些简单的东西,但是我没有在PHP中找到合适的解决方案。 I would like to ask for some advises, please. 我想请一些建议。

I have one list of servers in the Array/Vector called $lines, now I would like to SELECT each server name in a MySQL DB in order to collect additional infos. 我在Array / Vector中有一个服务器列表,称为$ lines,现在我想在MySQL DB中选择每个服务器名称,以收集其他信息。

Could you please give me ideas regarding how to do a loop from the server names in, to SELECT / QUERY each one ? 您能否给我一些关于如何从服务器名称循环到SELECT / QUERY每个循环的想法?

Thanks!! 谢谢!!

<html>
<head>

</head>
<body>

<form name="form1" method="post">
<textarea rows="10" name="servers[]" cols="50" ></textarea>
<input type="submit" name="submit" value="Submit" />
</form>

<?php
$con=mysqli_connect("hostxxxx","userxxxx","xxxxxxxx","xxxxxxx");
//Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  } 
if(isset($_POST["submit"]))
{
        if(!empty($_POST["servers"]))
        {
             echo '<h4>You have selected the following Servers:</4><br>';
            $submitted_array = array_keys($_POST["servers"]);                                                                  

       $lines = explode(PHP_EOL, $_POST["servers"][$submitted_array[0]]);
            foreach($lines as $servers)
            {           
        echo ($servers."<br>");
                }
}
else
{
echo 'Please write something';
}
}
?>
</body>
</html>

I've got the result as I needed, I hope it can help: 我得到了所需的结果,希望对您有所帮助:

<body>
<form name="form1" method="post">
<textarea rows="1000" name="servers[]" cols="100" style="width:300px;height:300px;"></textarea>
<input type="submit" name="submit" value="Submit" />
</form>

<?php
$con=mysqli_connect("hostxxxxx:portxx","userx","passxx","msdb");
//Check connection
if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }    
if(isset($_POST["submit"]))
{
   if(!empty($_POST["servers"]))
   {
           echo '<h4>You have selected the following Servers:</4><br>';
           $submitted_array = array_keys($_POST["servers"]);                                                                  

           //BREAKING IN AN ARRAY
    $lines = explode(PHP_EOL, $_POST["servers"][$submitted_array[0]]);
           //foreach($lines as $servers)
           //{            
        //echo ($servers."<br>");
           //}
//REMOVING BLANK SPACES:        
function trim_value(&$value) 
{
   $value = trim($value); 
}
//ADDING QUOTES BETWEEN THE VALUES
function add_quotes($str) {
   return sprintf("'%s'", $str);
}
//REMOVING BLANK LINES        
array_walk($lines, 'trim_value');        

$VCSAC2 = "VCSAC2";
$LPAR = "LPAR";
$HOSTF = "HOSTF";
$CLASSG = "CLASSG";
$IP = "IP";
$STATUS = "STATUS";


//USING IMPLODE        
//$sql=sprintf("SELECT * FROM main WHERE HOSTF IN (".implode(',', $lines).")");
$sql=sprintf("SELECT * FROM main WHERE HOSTF IN (".implode(',', array_map('add_quotes',$lines)).")");        
//USED TO CHECK SQL SINTAX        
//echo $sql;        

$result=mysqli_query($con,$sql);    
}
else
{
   echo 'Please write something';
}
}
?>

<table id="demo1" cellpadding="0" cellspacing="0" style="width:100%" style="font-size:13px">
        <thead>  
       <tr>
                   <th align=left>HostName</th>
                   <th align=left>VCS</th>
            <th align=left>LPAR</th>
            <th align=left>IP</th>
            <th align=left>Class</th>
        </tr>
        </thead>
           <?php while($dado = mysqli_fetch_array($result)) {?>

       <tbody>
       <tr>
        <td align=left style="font-size:14px"><?php echo $dado[$HOSTF]; ?></td>
        <td align=left style="font-size:14px"><?php echo $dado[$VCSAC2]; ?></td>
            <td align=left style="font-size:14px"><?php echo $dado[$LPAR]; ?></td>
            <td align=left style="font-size:14px"><?php echo $dado[$IP]; ?></td>
            <td align=left style="font-size:14px"><?php echo $dado[$CLASSG]; ?></td>
            </tr>
        </tbody>
           <?php } ?>
           </table> <br><br>
</body>

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

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